java发送邮件【雅虎邮箱.com】本地测试通过服务器上却不行?

JerryZhou_ JerryZhou_ 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
项目中有个java发邮件的功能,开始是gmail作为邮件发件箱的,后来要改成yahoo的 是.com的;
现在出现问题了,在本地测试可以发送邮件,上传代码到服务器上测试的时候却报错,
说是Could not connect to SMTP host: smtp.mail.yahoo.com, port: 25 连接超时什么的
请教各位....
下面是我的代码:
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
public static boolean SendEmailTest(String ToEmailStr, String Title,
String Content, String FromEmaila) throws AddressException {
String FromEmail = "lostcrownservice@yahoo.com";
boolean fnt = false;
Properties props = new Properties();
Session sendMailSession;
Transport transport;
sendMailSession = Session.getInstance(props, null);
String smtp = "smtp.mail.yahoo.com";
props.put("mail.smtp.host", smtp);
props.put("mail.smtp.auth", "true"); // 允许smtp校验
props.setProperty("mail.smtp.port", "25");

try {
transport = sendMailSession.getTransport("smtp");
transport.connect("smtp.mail.yahoo.com", "lostcrownservice@yahoo.com", "******"); Message newMessage = new MimeMessage(sendMailSession);

// 设置mail主题
String mail_subject = Title;
newMessage.setSubject(mail_subject);

// 设置发信人地址
Address address[] = { new InternetAddress(FromEmail) };// 改变发件人地址
newMessage.addFrom(address);
// 设置收件人地址
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(ToEmailStr));

// 设置mail正文
newMessage.setSentDate(new java.util.Date());
String mail_text = Content;
newMessage.setText(mail_text);

newMessage.saveChanges(); // 保存发送信息
transport.sendMessage(newMessage, newMessage.getRecipients(Message.RecipientType.TO));
fnt = true;
transport.close();
} catch (Exception e) {
e.printStackTrace();
fnt = false;
}
return fnt;
}
}
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
JerryZhou_
Java EE 67,535 成员 225,843 内容