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;
		}
	}

猜你喜欢

转载自venus224.iteye.com/blog/2221414