`
venus224
  • 浏览: 207956 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Could not connect to SMTP host: smtp.sina.com, port: 25

 
阅读更多
1.要在邮件设置中把POP3/SMTP服务开启。
2.如有安装要杀毒软件或防火墙,比如:McAFee 要在控制台->访问保护(右键)->属性->防病毒标准保护->禁止群发邮件蠕虫发送邮件->前面没有打钩
代码如下:
public boolean send(String to_mail_address,String subject,String content){
		try {
			String from_mail_address = userName;
		
			Authenticator auth = new PopupAuthenticator(userName, password);
			Properties mailProps = new Properties();
			mailProps.put("mail.smtp.host", smtp_server);
			mailProps.put("mail.smtp.auth", "true");
			mailProps.put("username", userName);
			mailProps.put("password", password);

			Session mailSession = Session.getDefaultInstance(mailProps, auth);
			mailSession.setDebug(true);
			MimeMessage message = new MimeMessage(mailSession);
			message.setFrom(new InternetAddress(from_mail_address));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(
					to_mail_address));
			message.setSubject(subject);

			MimeMultipart multi = new MimeMultipart();
			BodyPart textBodyPart = new MimeBodyPart();
			textBodyPart.setText(content);
			multi.addBodyPart(textBodyPart);
			message.setContent(multi);
			message.saveChanges();
			Transport.send(message);
			return true;
		} catch (Exception ex) {
			System.err.println("邮件发送失败的原因是:" + ex.getMessage());
			System.err.println("具体的错误原因");
			ex.printStackTrace(System.err);
			return false;
		}
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics