1. 程式人生 > >java實現郵箱發送郵件

java實現郵箱發送郵件

cep nds session clas bug and ack tex tac

第一步:封裝發件人賬號密碼

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
* 發件人賬號密碼
* @author zhangdi
*
*/
public class MailAuthenticator extends Authenticator{

public static String USERNAME = "";
public static String PASSWORD = "";

public MailAuthenticator() {
}

protected PasswordAuthentication getPasswordAuthentication() {


return new PasswordAuthentication(USERNAME, PASSWORD);
}

}

第二步:提供發送操作類

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;


import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;/**
* 郵件發送操作類
*
* @author zhangdi
*
*/
public class MailOperation {

/**
* 發送郵件
* @param user 發件人郵箱
* @param password 授權碼(註意不是郵箱登錄密碼)
* @param host
* @param from 發件人
* @param to 接收者郵箱
* @param subject 郵件主題


* @param content 郵件內容
* @return success 發送成功 failure 發送失敗
* @throws Exception
*/
public String sendMail(String user, String password, String host,
String from, String to, String subject, String content)
throws Exception {
if (to != null){
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
MailAuthenticator.USERNAME = user;
MailAuthenticator.PASSWORD = password;
Session session = Session.getInstance(props, auth);
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
if (!to.trim().equals(""))
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to.trim()));
message.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart(); // 正文
mbp1.setContent(content, "text/html;charset=utf-8");
Multipart mp = new MimeMultipart(); // 整個郵件:正文+附件
mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
message.setContent(mp);
message.setSentDate(new Date());
message.saveChanges();
Transport trans = session.getTransport("smtp");
trans.send(message);
System.out.println(message.toString());
} catch (Exception e){
e.printStackTrace();
return "failure";
}
return "success";
}else{
return "failure";
}
}

}

第三步:測試

public static void main(String[] args) {

        MailOperation operation = new MailOperation();
        String user = "你的郵箱地址";
        String password = "你的郵箱授權碼";
        String host = "smtp.163.com";
        String from = "你的郵箱地址";
        String to = "目標郵箱地址";// 收件人
        String subject = "輸入郵件主題";
        //郵箱內容
        StringBuffer sb = new StringBuffer();
        String yzm = RandomUtil.getRandomString(6);
        sb.append("<!DOCTYPE>"+"<div bgcolor=‘#f1fcfa‘   style=‘border:1px solid #d9f4ee; font-size:14px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px;   padding-bottom:5px;‘><span style=‘font-weight:bold;‘>溫馨提示:</span>"
                          + "<div style=‘width:950px;font-family:arial;‘>歡迎使用NET微活動,您的註冊碼為:<br/><h2 style=‘color:green‘>"+yzm+"</h2><br/>本郵件由系統自動發出,請勿回復。<br/>感謝您的使用。<br/>杭州恩意替電子商務有限公司</div>"
                         +"</div>");
        try {
            String res = operation.sendMail(user, password, host, from, to,
                    subject, sb.toString());
            System.out.println(res);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

java實現郵箱發送郵件