loki3328
2011-11-25 13:42
浏览 4.1k
已采纳

JAVAEMAIL 无法发送QQ企业邮箱

public static void main(String args[]){
sendEmail("XXX@qq.com", "你好", "ss");
}

public static void sendEmail(String touser,String title,String content){
    String host = "smtp.exmail.qq.com";// 邮件服务器地址
    String addresser = "service@XX.cn";// 发件人地址
    String username = "XXX";// 发件人邮箱用户名
    String password = "XXXXXX";// 发件人邮箱密码
    try {
        Properties props = new Properties();
        props.put("mail.smtp.host", host);// 设置smtp的邮件服务器地址
        props.put("mail.smtp.auth", "true");// 注意的是此处必须加上true要不然smtp连接的时候不会认证
        props.put("mail.transport.protocol", "smtp"); // 设置邮件的传输协议是smtp
        MyAuthenticator auth = new MyAuthenticator(username, password);
        // Session类定义了基本的邮件会话
        Session session = Session.getDefaultInstance(props, auth);// 创建一个Session
        MimeMessage message = new MimeMessage(session);
        // 设置发件人邮件地址
        message.setFrom(new InternetAddress(addresser));
        // 设置收件人邮件地址 Message.RecipientType.TO 设置发送的类型
        message.setRecipient(Message.RecipientType.TO,
                     ........

错误信息
serviceddservice...
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at com.jingdu.common.Function.sendEmail(Function.java:89)
at com.jingdu.common.Function.main(Function.java:54)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • huoyj 2011-11-25 14:27
    已采纳

    [code="java"]public class JavaMailTest
    {

    /**
     * @param args
     */
    
    private static final Logger logger = Logger.getLogger(Object.class);
    
    public static void main(String[] args) throws Exception
    {
        String userName = "xxxx@qq.com";
        // 下面是邮箱的密码
        String password = "xxxx";
        String host = "smtp.qq.com";
        String protocal = "smtp";
        String to = "xxxx@126.com";
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setRecipients(RecipientType.TO, to);
        message.setFrom(new InternetAddress(userName));
        message.setSubject("测试邮件");
        message.setText("Hello World ", "UTF-8");
        Transport transport = session.getTransport(protocal);
        transport.connect(host, userName, password);
        transport.sendMessage(message, message.getAllRecipients());
        System.out.println("--------send mail success-------------");
        logger.info("=============================");
    }
    

    [/code]
    这个我测试成功了,但你首先要开启你的qq邮箱的SMTP服务,
    设置->帐户->POP3/IMAP/SMTP服务,里面的开启POP3/SMTP服务和开启IMAP/SMTP服务勾选上。

    点赞 打赏 评论
  • iteye_13395 2011-11-25 13:43

    看这个错误是说授权错误,检查一下用户名和密码是否正确

    点赞 打赏 评论
  • iteye_13395 2011-11-25 13:45

    另外

    1、QQ的smtp的地址是smtp.qq.com

    2、QQ邮箱的需要在邮箱的帐户里面将这个打开,记得默认是关闭的

    点赞 打赏 评论
  • yy1230 2011-11-25 13:47

    String host = "smtp.exmail.qq.com";// 邮件服务器地址
    这个地址有问题吧

    试试QQ邮箱

    接收服务器:pop.qq.com

    发送服务器:smtp.qq.com

    点赞 打赏 评论
  • huoyj 2011-11-25 14:13

    import java.util.Properties;

    import javax.mail.Message.RecipientType;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    import org.apache.log4j.Logger;

    public class JavaMailTest {

    /**
     * @param args
     */
        private  String userName="service@XX.cn";
       //下面是yanlong137@hotmail.com这个邮箱的密码
        private  String password ="********";
    private  String host = "smtp.qq.com";
    private  String protocal = "smtp";
        private  String to = "xxxxxxx.com";
    
    private static final Logger logger = Logger.getLogger(Object.class);
    
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setRecipients(RecipientType.TO, to);
        message.setFrom(new InternetAddress(userName));
        message.setSubject("测试邮件");
        message.setText("Hello World ","UTF-8");
        Transport transport = session.getTransport(protocal);
        transport.connect(host,userName,password);
        transport.sendMessage(message, message.getAllRecipients());
        System.out.println("--------send mail success-------------");
        logger.info("=============================");
    }
    

    }

    点赞 打赏 评论

相关推荐 更多相似问题