import java.util.Properties;html
import javax.mail.Authenticator;java
import javax.mail.Message;服務器
import javax.mail.MessagingException;session
import javax.mail.PasswordAuthentication;ide
import javax.mail.Session;字體
import javax.mail.Transport;ui
import javax.mail.internet.AddressException;編碼
import javax.mail.internet.InternetAddress;spa
import javax.mail.internet.MimeMessage;orm
public class MySendMessag2 {
/**
* 郵件發送
*/
public static void send()
{
//會話屬性設置
Properties properties=new Properties();
//身份驗證協議設置
properties.setProperty("mail.smtp.auth", "true");
//發送協議設置
properties.setProperty("mail.transport.protocol", "smtp");
//設置主機服務器
properties.setProperty("mail.host", "smtp.qq.com");
//創建會話機制,注入屬性
Session session=Session.getInstance(properties,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//郵箱密碼設置
return new PasswordAuthentication("用戶名", "密碼");
//信息的創建
Message message=new MimeMessage(session);
//發送方的郵箱的設置
try {
message.setFrom(new InternetAddress("發送方地址"));
} catch (AddressException e) {
System.out.println("發送方郵箱設置失敗");
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//設置主題
try {
message.setSubject("美好的主題");
} catch (MessagingException e1) {
System.out.println("主題設置有誤");
e1.printStackTrace();
}http://www.huiyi8.com/font/ 字體
try {//設置發送內容,將格式設置爲html或文本格式,編碼爲UTF-8
message.setContent("<a color='red' href='www.baidu.com'>你</a>","text/html;charset=UTF-8 " );
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
//接收方郵箱的設置,能夠同時有多個接收方,有抄送(cc)和密送(bcc),收件人
try {//按收件人發送給接收方一,接收方二
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
"接收方一,「接收方2」"));
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//跟蹤發送信息
session.setDebug(true);
//發送信息開始
try {
Transport.send(message);
} catch (MessagingException e) {
System.out.println("發送失敗");
e.printStackTrace();
* @param args
*/
public static void main(String[] args) {
send();