JAVAMAIL发送SMTP认证的邮件
2007-12-28 13:11
627 查看
网路上很多JAVAMAIL发送邮件的代码,比较杂乱,很多代码把附件,抄送都加上去了,对于初学JAVAMAIL的人可能不太有用,大多用到JAVAMAIL的程序,可能只是需要发发邮件(一般都是写一个自动发邮件的功能),这里给大家一个比较简单的需要认证的邮件发送的代码。
首先导入mail.jar和activation.jar这两个包。
import java.io.IOException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail {
public static void send(String body) throws MessagingException, AddressException, IOException {
String from = xxxxx@hp.com;
String to = yyyyy@alcatel-sbell.com;
String subject = "mail subject";
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.hp.cpm");//这里请输入SMTP,可以使用新浪的。
props.put("mail.smtp.auth", "true");//这里写false的话,说明此邮件不需要验证
Session session = Session.getDefaultInstance(props,
new PasswordAuthenticator(from, "password"));
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,
false));
msg.setSubject(subject);
msg.setText(body);
msg.setSentDate(new Date());
Transport.send(msg);
}
}
class PasswordAuthenticator extends Authenticator {
private String username;
private String password;
public PasswordAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
代码已经写好了 ,如果需要测试 ,直接调用send(String.....)方法就可以了。
首先导入mail.jar和activation.jar这两个包。
import java.io.IOException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail {
public static void send(String body) throws MessagingException, AddressException, IOException {
String from = xxxxx@hp.com;
String to = yyyyy@alcatel-sbell.com;
String subject = "mail subject";
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.hp.cpm");//这里请输入SMTP,可以使用新浪的。
props.put("mail.smtp.auth", "true");//这里写false的话,说明此邮件不需要验证
Session session = Session.getDefaultInstance(props,
new PasswordAuthenticator(from, "password"));
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to,
false));
msg.setSubject(subject);
msg.setText(body);
msg.setSentDate(new Date());
Transport.send(msg);
}
}
class PasswordAuthenticator extends Authenticator {
private String username;
private String password;
public PasswordAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
代码已经写好了 ,如果需要测试 ,直接调用send(String.....)方法就可以了。
相关文章推荐
- 使用JavaMail发送SMTP认证的邮件给多个收信人
- 使用spring javamail smtp发送html邮件
- 在VB6.0中使用Socket发送带SMTP认证的邮件
- 在VB6.0中使用Socket发送带SMTP认证的邮件
- python smtplib发送邮件遇到的认证问题
- 发送需要smtp认证的邮件
- Log4j发送需SMTP认证邮件的配置方法
- 【转】javamail-基于SMTP的JAVA邮件发送程序!
- javamail技术smtp发送邮件
- 在VB6.0中使用Socket发送带SMTP认证的邮件
- perl利用SMTP发送邮件脚本(带认证)
- java发送邮件(javaMail通过SMTP发送邮件)
- Postfix邮件服务器的原理和postfix + dovecot配置,使用Thunderbird发送邮件和SMTP认证
- javamail的smtp方式发送邮件实例
- Spring中javaMail通过SMTP发送邮件
- Spring中javaMail通过SMTP发送邮件
- 在VB6.0中使用Socket发送带SMTP认证的邮件
- 在VB6.0中使用Socket发送带SMTP认证的邮件
- 使用System.Web.Mail 组件通过SMTP认证发送邮件
- c#发送需要smtp认证的邮件