java后端代碼實現(xiàn)發(fā)送郵件但是有時候沒響應是jar包的問題。根據查詢相關公開信息:要確保郵箱已經開啟了POP3、SMTP等服務,開啟之后,一定要生成授權碼,上面的郵箱名和密碼。

創(chuàng)新互聯(lián)建站,專注為中小企業(yè)提供官網建設、營銷型網站制作、響應式網站開發(fā)、展示型成都做網站、網站設計等服務,幫助中小企業(yè)通過網站體現(xiàn)價值、有效益。幫助企業(yè)快速建站、解決網站建設與網站營銷推廣問題。
可以使用以下方法將JSON文件發(fā)送給客戶端:
1. 將JSON文件讀取為字符串,例如:
```
String jsonString = new String(Files.readAllBytes(Paths.get("path/to/jsonFile.json")));
```
2. 將字符串設置為響應體,設置響應頭為JSON格式,例如:
```
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonString);
```
3. 發(fā)送響應,例如:
```
response.flushBuffer();
```
另外還可以使用一些框架,如Spring MVC的`@ResponseBody`注解,可以將JSON對象或實體類自動轉換為JSON格式發(fā)送給客戶端。
方法:
1.前提準備工作:
首先,郵件的發(fā)送方要開啟POP3 和SMTP服務--即發(fā)送qq郵件的賬號要開啟POP3 和SMTP服務
2.開啟方法:
登陸qq郵箱
3.點擊 設置
4.點擊—-賬戶
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 —點擊開啟
6.送短信 —–點擊確定
7.稍等一會,很得到一個授權碼! –注意:這個一定要記住,一會用到
8.點擊保存修改 —OK 完成
9.java 測試代碼:
package cn.cupcat.test;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 連接協(xié)議
properties.put("mail.smtp.host", "smtp.qq.com");// 主機名
properties.put("mail.smtp.port", 465);// 端口號
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//設置是否使用ssl安全連接 ---一般都使用
properties.put("mail.debug", "true");//設置是否顯示debug信息 true 會在控制臺顯示相關信息
//得到回話對象
Session session = Session.getInstance(properties);
// 獲取郵件對象
Message message = new MimeMessage(session);
//設置發(fā)件人郵箱地址
message.setFrom(new InternetAddress("123456789@qq.com"));
//設置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });
//設置郵件標題
message.setSubject("這是第一封Java郵件");
//設置郵件內容
message.setText("內容為: 這是第一封java發(fā)送來的郵件。");
//得到郵差對象
Transport transport = session.getTransport();
//連接自己的郵箱賬戶
transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密碼為剛才得到的授權碼
//發(fā)送郵件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.運行就會發(fā)出郵件了。。。。
下面是我收到郵件的截圖,當然我把源碼中的郵件地址都是修改了,不是真實的,你們測試的時候,可以修改能你們自己的郵箱。最后,祝你也能成功,如果有什么問題,可以一起討論!
注意事項
得到的授權碼一定要保存好,程序中要使用
public boolean mainto()
{
boolean flag = true;
//建立郵件會話
Properties pro = new Properties();
pro.put("mail.smtp.host","smtp.qq.com");//存儲發(fā)送郵件的服務器
pro.put("mail.smtp.auth","true"); //通過服務器驗證
Session s =Session.getInstance(pro); //根據屬性新建一個郵件會話
//s.setDebug(true);
//由郵件會話新建一個消息對象
MimeMessage message = new MimeMessage(s);
//設置郵件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;
try
{
fromAddr = new InternetAddress(451144426+"@qq.com"); //郵件發(fā)送地址
message.setFrom(fromAddr); //設置發(fā)送地址
toAddr = new InternetAddress("12345367@qq.com"); //郵件接收地址
message.setRecipient(Message.RecipientType.TO, toAddr); //設置接收地址
message.setSubject(title); //設置郵件標題
message.setText(content); //設置郵件正文
message.setSentDate(new Date()); //設置郵件日期
message.saveChanges(); //保存郵件更改信息
Transport transport = s.getTransport("smtp");
transport.connect("smtp.qq.com", "451144426", "密碼"); //服務器地址,郵箱賬號,郵箱密碼
transport.sendMessage(message, message.getAllRecipients()); //發(fā)送郵件
transport.close();//關閉
}
catch (Exception e)
{
e.printStackTrace();
flag = false;//發(fā)送失敗
}
return flag;
}
這是一個javaMail的郵件發(fā)送代碼,需要一個mail.jar
網頁標題:java代碼發(fā)送 java代碼發(fā)送短信
鏈接URL:http://chinadenli.net/article46/dojgpeg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、商城網站、網站維護、營銷型網站建設、網站設計公司、品牌網站制作
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)