spring 40天全程跟踪 第一天spring发送邮件

阅读更多
com.sun.mail.smtp.SMTPSendFailedException: 553 You are not authorized to send mail, authentication is required
邮件发送成功

	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1275)
	at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:895)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:524)
	at javax.mail.Transport.send0(Transport.java:151)
	at javax.mail.Transport.send(Transport.java:80)
	at com.mixian.springEmail.MainEmail.send(MainEmail.java:113)
	at com.mixian.springEmail.MainEmail.main(MainEmail.java:138)
 spring发送邮件: package com.mixian.springEmail; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Enumeration; import java.util.Properties; import java.util.Vector; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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; import javax.mail.internet.MimeUtility; public class MainEmail { //收件人 private String to=""; //发件人 private String from=""; //smtp服务器 private String smtpServer=""; //用户名 private String username=""; //密码 private String pwd=""; //附件文件名 private String filename=""; //邮件主题 private String subject=""; //邮件正文 private String content=""; //附件文件集合 Vector file = new Vector(); //构造函数 public MainEmail(){} public MainEmail(String to,String from,String smtpServer,String username,String pwd, String filename,String subject,String content){ this.to = to; this.from = from; this.smtpServer = smtpServer; this.username = username; this.pwd = pwd; this.filename = filename; this.subject = subject; this.content = content; } //邮件主题转换成中文 public String transferChinese(String strText){ try { strText = MimeUtility.encodeText(new String(strText.getBytes(),"GB2312"), "GB2312", "B"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return strText; } //增加附件 public void attachfile(String fname){ file.addElement(fname); } //发送邮件 public boolean send(){ //创建邮件session所需的对象 Properties props = System.getProperties(); props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.auth", "true"); //创建session Session session = Session.getDefaultInstance(props, new Authenticator(){ public PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username,pwd); } }); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); //构造Multipart Multipart mp = new MimeMultipart(); //添加正文 MimeBodyPart mbpContent = new MimeBodyPart(); mbpContent.setText(content); mp.addBodyPart(mbpContent); //向Multipart添加附件 Enumeration efile = file.elements(); while(efile.hasMoreElements()){ MimeBodyPart mbpFile = new MimeBodyPart(); filename = efile.nextElement().toString(); FileDataSource fds = new FileDataSource(filename); //处理附件 mbpFile.setDataHandler(new DataHandler(fds)); mbpFile.setFileName(fds.getName()); //向MimeMessage mp.addBodyPart(mbpFile); } file.removeAllElements(); msg.setContent(mp); msg.setSentDate(new Date(0)); Transport.send(msg); } catch (MessagingException e) { e.printStackTrace(); /*Exception ex=null; if((ex==e.getNextException())!=null){ ex.printStackTrace(); }*/ return false; } return true; } public static void main(String[] args) { MainEmail sendMail = new MainEmail(); sendMail.setSmtpServer("smtp.163.com"); sendMail.setUsername("xx"); sendMail.setPwd("xx"); sendMail.setTo("xx"); sendMail.setFrom("[email protected]"); sendMail.setSubject("test spring email"); sendMail.setContent("这是一个带多附件的测试"); sendMail.attachfile("D:\test.txt"); sendMail.attachfile("D:\test.txt"); sendMail.send(); System.out.println("邮件发送成功"); } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getSmtpServer() { return smtpServer; } public void setSmtpServer(String smtpServer) { this.smtpServer = smtpServer; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Vector getFile() { return file; } public void setFile(Vector file) { this.file = file; } }

 操作结果报错:

 

com.sun.mail.smtp.SMTPSendFailedException: 553 You are not authorized to send mail, authentication is required
邮件发送成功

	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1275)
	at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:895)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:524)
	at javax.mail.Transport.send0(Transport.java:151)
	at javax.mail.Transport.send(Transport.java:80)
	at com.mixian.springEmail.MainEmail.send(MainEmail.java:113)
	at com.mixian.springEmail.MainEmail.main(MainEmail.java:138)

 解决方法:用户登录的邮件名 要与发送的邮件名一样

你可能感兴趣的:(Spring,Java,SUN)