做一个BAT程序用JAVA,需要能实现发送邮件的功能。
大家谁有相应的代码提供下,APATCH下有什么包支持发送邮件的吗
大家谁有相应的代码提供下,APATCH下有什么包支持发送邮件的吗
解决方案 »
- web开发的框架
- 很想从事JAVA,请大家给给建议。
- String date="1905-05-23 00:00:00.0";怎么获取前面的1905-05-23 一串字符?????????????????????????
- HTTP Status 500 帮忙解决下t_t
- 新年要找工作了,大家谈谈找工作的体会吧,谈些面试上的技术上的东西也行,不限话题
- 有谁能解释这个难题的?
- 如何得到一个网页的header信息?
- xiaofenguser(风雨) 乱码???????问题请进啊!!
- 谁能告诉我jrun studio开发jsp的配置过程?
- 谁有IBM的VISUAL AGE给小弟发一个,急用。谢了。[email protected]
- 如何添加JSTL标签库
- 页面中播放MP4代码怎么写啊
我的QQ主页 已测试过
不带附件的 带附件 HTML格式的都有
谢谢
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSender {
private String smtp = "smtp.qq.com"; // 邮件服务器
private String username = "11111";
private String password = "xxxxx";
private String from = username+"@qq.com";
EmailAuthenticator auth = new EmailAuthenticator(this.username,this.password);
Properties props = System.getProperties();
{
props.put("mail.smtp.host",smtp);
props.put("mail.smtp.auth", "true");
}
Session session = Session.getDefaultInstance(props,(Authenticator)auth);
Message msg = new MimeMessage(session);
public void sendTo(String to){
try{
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("用户注册验证");
msg.setSentDate(new Date());
msg.setText("xxxxx");
Transport transport = session.getTransport("smtp");
transport.connect(smtp, from, password);
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
System.out.println("OK");
}catch(Exception e){
}
}
public static void main(String[] args) {
new MailSender().sendTo("[email protected]");
}
}
class EmailAuthenticator extends Authenticator{
private String username, password;
public EmailAuthenticator(String username, String password){
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(this.username, this.password);
}
}另外 java 需要的jar 在 java ee 5 中已经带了 , 不需要额外下载吧
import javax.mail.*;
import javax.mail.internet.*;public class MailExample {
public static void main(String args[]) throws Exception, MessagingException {
String host = "smtp.163.com";//发送邮件服务器
String from = "发送者的邮箱地址";
String to = "接受者的邮箱地址"; // new properties
Properties props = new Properties(); // Setup mail server
props.put("mail.smtp.host", host);// 设置smtp主机 props.put("mail.smtp.auth", "true");// 使用smtp身份验证 // Get session
Session session = Session.getDefaultInstance(props, null); // Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("test");//标题
message.setText("http://www.baidu.com/"+"This is test JavaMail");//内容
//http协议部分会自动转换成超链接
message.saveChanges(); // Send message
Transport transport = session.getTransport("smtp");
System.out.println("正在连接");
transport.connect(host, "发送者邮箱地址/用户名", "发送者密码");
System.out.println("正在发送");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("邮件发送成功");
}
}