欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java mail 发送邮件

程序员文章站 2022-05-25 13:01:41
...

 

public class JavaMailTest {

	public static void main(String args[]) throws MessagingException {
		String smtpHost = "smtp.sohu.com";
		String from = "[email protected]";
		String to = "[email protected]";

		Properties properties = System.getProperties();
		Session session = Session.getInstance(properties);

		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		message.setSubject("JavaMail Example");
		message.setText("Did it work?");
		Transport transport = session.getTransport("smtp");
		transport.connect(smtpHost, "", "");

		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}
}

 

可能会出现如下错误

'550 5.1.1 <[email protected]>: Recipient address rejected: User unknown in virtual mailbox table',

你发送的帐号在邮件系统内不存在,请检查你的邮件地址是否输入有误

 

'554 5.7.1 <[email protected]>: Sender address rejected: Access denied',

你的邮箱是内部帐号,你所发送的地址不在授权域之内

 

'553 5.7.1 <[email protected]>: Sender address rejected: not logged in',

发送邮件需要SMTP身份验证,你的帐号SMTP身份验证部分没有设置正确,请检查配置

 

'454 4.7.1 <unknown[***.***.***.***]>: Client host rejected: Access denied'

你的IP被管理员屏蔽,请检查你部的机器是否被感染病毒自动发送邮件