问题描述
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"); // 设置邮件的传输协议是smtpMyAuthenticator auth = new MyAuthenticator(username, password);// Session类定义了基本的邮件会话Session session = Session.getDefaultInstance(props, auth);// 创建一个SessionMimeMessage message = new MimeMessage(session);// 设置发件人邮件地址message.setFrom(new InternetAddress(addresser));// 设置收件人邮件地址 Message.RecipientType.TO 设置发送的类型message.setRecipient(Message.RecipientType.TO, ........错误信息serviceddservice...javax.mail.AuthenticationFailedExceptionat 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) 问题补充:如何设置发件人的用户名的?比如我用abc@company.cn ,我发的用户名显示成“公司“.你的程序测试有用,谢啦 。但不知道为什么我的就不行,163发可以,QQ普通邮件发也可以。就是用QQ企业邮件发就不行。
解决方案
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("=============================");}这个我测试成功了,但你首先要开启你的qq邮箱的SMTP服务,设置->帐户->POP3/IMAP/SMTP服务,里面的开启POP3/SMTP服务和开启IMAP/SMTP服务勾选上。
解决方案二:
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("=============================");}}
解决方案三:
String host = "smtp.exmail.qq.com";// 邮件服务器地址 这个地址有问题吧试试QQ邮箱接收服务器:pop.qq.com发送服务器:smtp.qq.com
解决方案四:
另外1、QQ的smtp的地址是smtp.qq.com2、QQ邮箱的需要在邮箱的帐户里面将这个打开,记得默认是关闭的
解决方案五:
看这个错误是说授权错误,检查一下用户名和密码是否正确