javaMail在main方法中发送邮件显示成功,但是邮箱里收不到,是怎么回事?
web程序发送的邮件正常。
import java.util.Properties; import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; public class SendMail {
public static void main(String[] args) throws Exception { Properties props = new Properties();
// props.setProperty("mail.debug", "true");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true); Message msg = new MimeMessage(session);
msg.setText("您好啊!");
msg.setFrom(new InternetAddress("[email protected]")); Transport transport = session.getTransport();
transport.connect("smtp.sina.com", 25, "zlqfjhome", "fujin830925");
transport.sendMessage(msg, new Address[] { new InternetAddress(
"[email protected]") });
transport.close();
} }
web程序发送的邮件正常。
import java.util.Properties; import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; public class SendMail {
public static void main(String[] args) throws Exception { Properties props = new Properties();
// props.setProperty("mail.debug", "true");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true); Message msg = new MimeMessage(session);
msg.setText("您好啊!");
msg.setFrom(new InternetAddress("[email protected]")); Transport transport = session.getTransport();
transport.connect("smtp.sina.com", 25, "zlqfjhome", "fujin830925");
transport.sendMessage(msg, new Address[] { new InternetAddress(
"[email protected]") });
transport.close();
} }
解决方案 »
- spring 配置问题 No Scope registered for scope 'singletone'救命啊
- 关于JSONObject解析json数据,获取不到指定key的问题
- jsp链接数据库,tomcat不会报错,帮看看哪里错了
- 查找发贴最多的用户,用HQL怎么写啊~~一个下午了写不出啊~~
- 朋友们谁公司要人能帮忙推荐下,万分感谢。2年互联网开发经验,简历我写到内容里了。
- 寻JSP平台搭建的各种工具!
- 如何获取用户登陆的IP?
- 新手不会配置,大家帮手.给分100.
- 刚才成功了,怎么现在又不行了??????????????????????????????
- jsp中可以弹出一个对话框嘛??就像javascript中alert一样。。
- 怎样把第一个html页面的用户数据传送给第二个html文件,但是不显示这个值在url中
- IIS+Tomcat5.5整合问题?
换个SMTP服务器试试吧,或者在本机运行一个,这样便于查看邮件转发日志。
public class SendMail { private String sender = "[email protected]";
private String smtpHost = "smtp.sina.com";
private String user = "[email protected]";
private String password = "wdmmssm";
private void sendBody(String mailTo, String subject, String information)
throws Exception, MessagingException {
Properties prop = new Properties();
prop.put("mail.smtp.host", smtpHost);// 设置smtp主机
prop.put("mail.smtp.auth", "true");// 使用smtp身份验证
prop.put("mail.smtp.user", user);
prop.put("mail.smtp.password", password);
Session session = Session.getInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(sender, password);
}
});
// 获得邮件会话对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
// 设置收件人地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
// 设置邮件主题
message.setSubject(subject);
// mail内容部分
MimeBodyPart part = new MimeBodyPart();
part.setText(information == null ? "" : information, "UTF-8");
// 设置邮件格式为html
part.setContent(information.toString(), "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(part);// 在 Multipart 中增加mail内容部分
message.setContent(multipart);// 增加 Multipart 到信息体
message.setSentDate(new Date());// 设置发送日期
Transport.send(message);// 发送邮件
} public void send(String mailTo, String subject, String information) {
try {
this.sendBody(mailTo, subject, information);
} catch (Exception e) {
e.printStackTrace();
}
}}
看看吧。我是把它写到一个类里面了。什么地方需要时 直接调用这个类的方法就可以用了。很简单的。.