標籤:list 郵件伺服器 sendmail content 密碼 utf8 ext cat use
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sendmail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author hgh
*/
public class sdm {
public static void main(String[] args) {
System.out.println("啟動成功");
try {
int port = 25;
String server = "smtp.163.com";//郵件伺服器mail.cpip.net.cn
// String from = "[email protected]";//寄件者,顯示的寄件者名字
String from = "[email protected]";//寄件者,顯示的寄件者名字
// String user = "[email protected]";//寄件者郵箱地址
String user = "[email protected]";//寄件者郵箱地址
String password = "NMFsqa6HnV1t91Iu";//密碼
//建立會話
Properties props = new Properties();
props.put("mail.smtp.host", server);
props.put("mail.smtp.port", String.valueOf(port));
props.put("mail.smtp.auth", "true");
Transport transport = null;
Session session = Session.getInstance(props);
//建立資訊
Message msg = new MimeMessage(session);
//寄件者
msg.setFrom(new InternetAddress(from));
String toList = "[email protected]";
InternetAddress[] iaToList = new InternetAddress().parse(toList);
//收件者
msg.setRecipients(Message.RecipientType.TO, iaToList);
// 發送日期
msg.setSentDate(new Date());
// 主題
msg.setSubject("測試標題");
msg.setContent("測試內容", "text/html;charset=utf8"); //內容
// 郵件伺服器進行驗證
transport = session.getTransport("smtp");
transport.connect(server, user, password);
// bluebit_cn是使用者名稱,xiaohao是密碼
transport.sendMessage(msg, msg.getAllRecipients()); // 發送
System.out.println("郵件發送成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
java 郵箱發送