yxhxj2006

常用链接

统计

最新评论

用gmail smtp发送邮件

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;

public class SendMail implements Runnable {
    private final String smtp_host = "smtp.gmail.com";                      
    private final String from_userName = "xx@gmail.com";        
    private final String from_passWord = "xx";                        
    private final String show_name = "xxx";
    
    public String email_address;
    public String email_subject;
    public String email_content;    
    
    public SendMail(String email_address, String email_subject, String email_content) {
        this.email_address = email_address;
        this.email_subject = email_subject;
        this.email_content = email_content;
    }

    public void run() {
        executeMailSend(email_address, email_subject, email_content);
    }
    
    /**
     * 
     * 
@param recipients: the mail send to
     * 
@param sendSubject: the mail subject
     * 
@param sendText: the mail content
     
*/
    public void executeMailSend(String recipients, String sendSubject, String sendText) {
        
        try {            
            Properties props = System.getProperties();
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", smtp_host);
            props.put("mail.smtp.user", from_userName);
            props.put("mail.smtp.password", from_passWord);
            props.put("mail.smtp.port", "587");  //gmail smtp port 587
            props.put("mail.smtp.auth", "true");
            

            
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(false);
            
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from_userName));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
            message.setFrom(new InternetAddress(show_name + "<" + from_userName + ">"));    
            message.setSubject(sendSubject);
            message.setContent(sendText, "text/html;charset=utf-8");            
            
            Transport transport = session.getTransport("smtp");
            transport.connect(smtp_host, from_userName, from_passWord);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            
            System.out.println("send an email to " + recipients + " success");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("failure! ");

        }
    }
    public static void main (String args[]){
        new SendMail("yy@gmail.com","test","hehe").run();
    }
}
结果老是报如下的错误
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 36sm443504yxh.67
failure! 

    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
    at SendMail.executeMailSend(SendMail.java:59)
    at SendMail.run(SendMail.java:25)
    at SendMail.main(SendMail.java:70)
 折腾了个把小时,苦苦寻求后发现一个解决办法 
props.put("mail.smtp.port""587");  
替换成
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
props.setProperty("mail.smtp.port", "465"); 
props.setProperty("mail.smtp.socketFactory.port", "465"); 
究其原因也不太清楚,网上有资料说port 587也可以,不知结果不行
把上述代码中的 

posted on 2013-03-08 15:16 奋斗成就男人 阅读(1131) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航: