javamail和apache email发送google apps企业套件非gmail域名的邮箱作为smtp发email邮件

对于使用google apps 企业免费套件注册的非gmail域名的gmail邮箱(如:[email protected])作为smtp发送电子邮件,javamail不能完成,始终报
javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at                  
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 t1sm6758913rvl.9

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at com.maweis.util.MailUtil.sendEMail(MailUtil.java:84)
at com.maweis.util.MailUtil.main(MailUtil.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
异常。

但是使用apache email 发送则不会报异常。

package com.xxxx.util;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

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 java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2010-6-23
* Time: 9:34:14
* java发email
*/
public class MailUtil {
    public static void main(String[] args) {
    //javamail 发邮件
        MailUtil.sendEMail("[email protected]","您的密码","您的密码为:123456");

        //apache email 发邮件
        MailUtil.send();

    }

    /**
     * <p>发送电子邮件</p>
     *
     * @param to      接收者的email地址
     * @param subject eamil标题
     * @param body    email内容
     * @return
     */
    public static boolean sendEMail(String to, String subject, String body) {
        //aabbcc
        String smtphost = "smtp.gmail.com"; // 发送邮件服务器
        String user = "tom"; // 邮件服务器登录用户名
        String password = "xxxxxx"; // 邮件服务器登录密码
        String from = "[email protected]"; // 发送人邮件地址


// 以下为发送程序,用户无需改动
        try {
            System.out.println("eamil:" + to);           
            Properties props = new Properties();
            props.put("mail.smtp.host", smtphost);
            props.put("mail.smtp.auth", "true");
            if (smtphost.indexOf("smtp.gmail.com") >= 0) {
                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");
            }
            javax.mail.Session ssn = javax.mail.Session.getInstance(props, null);
            MimeMessage message = new MimeMessage(ssn);
            InternetAddress fromAddress = new InternetAddress(from);
            message.setFrom(fromAddress);
            InternetAddress toAddress = new InternetAddress(to);
            message.addRecipient(Message.RecipientType.TO, toAddress);
            message.setSubject(subject);
            message.setText(body);
            Transport transport = ssn.getTransport("smtp");
            transport.connect(smtphost, user, password);
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
            transport.close();
            return true;
        } catch (Exception m) {
            m.printStackTrace();
        }
        return false;
    }
   
//apache email
    public static void send() {
        SimpleEmail email = new SimpleEmail();

        try {
//            email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验
            email.setSSL(true);
            email.setHostName("smtp.gmail.com");
            email.setAuthentication("[email protected]", "xxxxxx");   //用户名和密码
            email.setSslSmtpPort("465");
            email.setFrom("[email protected]", "xxxxxx");
            email.setCharset("gbk");
            email.addTo("[email protected]"); //接收方
//                email.addCc("[email protected]"); //抄送方
//                email.addBcc("[email protected]"); //秘密抄送方
            email.setSubject("Java Mail Test");         //标题
            email.setMsg("Just a simple send test .");   //内容
            email.send();
           
        } catch (EmailException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java,apache,qq,Google,Gmail)