求一段javamail代码,能用立即给分,在线等~~~

uf008 2008-05-06 02:40:46
求一段javamail代码,能用立即给分,在线等,先到先得~~~
...全文
116 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
uf008 2008-05-06
  • 打赏
  • 举报
回复
谢谢
胡矣 2008-05-06
  • 打赏
  • 举报
回复
以前写的一个例子

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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 JavaMailFor163 {
public static void main(String[] args) throws AddressException,
MessagingException {
String to = "***@163.com";
String from = "***@163.com";
Properties props = new Properties();
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", "smtp.163.com"); // 这里填写你发信者的SMTP主机,如:smtp.sohu.com
// props.put("mail.smtp.user", usr);
// props.put("mail.smtp.password", pwd);
props.put("mail.smtp.auth", "true");
sendMailSession = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("邮箱地址","邮箱密码");
}// 这里填写你发信者的邮箱地址和密码
});// 如果你的邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
to));
newMessage.setSubject("Stamp");
newMessage.setSentDate(new Date());
newMessage.setText("Stamp");
transport = sendMailSession.getTransport("smtp");
Transport.send(newMessage);
}
}

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧