发纯文本的邮件
首先下载commons-email-1.1.jar、mail.jar和activation.jar(它们可以从sun的网站上下载,从下载的javamail和jaf中找到)。
(一)使用javax.mail.*
import java.io.UnsupportedEncodingException;
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;
/**
* @description
* @author lyf
* @createTime Jul 28, 2011
* @version 1.1.1.1
*/
public class SendTextMail {
String SMTPHost="";//SMTP服务器
String user="";//登录SMTP服务器的账号
String passwd;
String from="";//发件人邮箱
String to="";//收件人邮箱
String subject="";//邮件标题
String content="";//邮件内容
public SendTextMail()
{
}
/**
* 发送邮件
* @createTime Jul 28, 2011
*/
public void sendMail()
{
Properties props=new Properties();
props.put("mail.smtp.host", SMTPHost);
props.put("mail.smtp.auth", "true");
try {
SmtpAuth auth=new SmtpAuth();
auth.setAccount(user, passwd);
//create a Session
Session mailSession=Session.getDefaultInstance(props,auth);
mailSession.setDebug(true);
//create a Message Object
Message message=new MimeMessage(mailSession);
//sender
message.setFrom(new InternetAddress(from));
//getter
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//subject
message.setSubject(subject);
//set the content
message.setText(content);
//send time
message.setSentDate(new Date());
//priority 1:urgency 2:general 3:slow
message.setHeader("X-Priority", "1");
message.saveChanges();
//create a Transport Object
Transport transport=mailSession.getTransport("smtp");
//connect to SMTP Server
transport.connect(SMTPHost, user, passwd);
//sendMail
transport.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
e.printStackTrace();
}
}
static class SmtpAuth extends Authenticator
{
String user,passwd;
//setting the user information
void setAccount(String user,String passwd)
{
this.user=user;
this.passwd=passwd;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return (new PasswordAuthentication(user,passwd));
}
}
/**
* @return the sMTPHost
*/
public String getSMTPHost() {
return SMTPHost;
}
/**
* @param host the sMTPHost to set
*/
public void setSMTPHost(String host) {
SMTPHost = host;
}
/**
* @return the user
*/
public String getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(String user) {
this.user = user;
}
/**
* @return the from
*/
public String getFrom() {
return from;
}
/**
* @param from the from to set
*/
public void setFrom(String from) {
this.from = from;
}
/**
* @return the to
*/
public String getTo() {
return to;
}
/**
* @param to the to to set
*/
public void setTo(String to) {
this.to = to;
}
/**
* @return the subject
*/
public String getSubject() {
return subject;
}
/**
* @param subject the subject to set
*/
public void setSubject(String subject) {
try {
subject=new String(subject.getBytes("ISO8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.subject = subject;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
try {
content=new String(content.getBytes("ISO8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content = content;
}
/**
* @return the passwd
*/
public String getPasswd() {
return passwd;
}
/**
* @param passwd the passwd to set
*/
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
(二)使用org.apache.commons.mail.*(此方法比方法一简单很多)
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class Send
{
private String SMTPHost="";//SMTP服务器
private String user="";//登录SMTP服务器的账号
private String passwd;
private String from="";//发件人邮箱
private String to="";//收件人邮箱
private String subject="";//邮件标题
private String content="";//邮件内容
public Send(String host, String user, String passwd,String to, String subject, String content) {
SMTPHost = host;
this.user = user;
this.passwd = passwd;
this.from = user;
this.to = to;
this.subject = subject;
this.content = content;
}
public static void main(String[] args)
{
Send send=new Send("smtp.163.com","****@163.com","******","**email_address**","","");
send.setSubject("注册成功!!!");
send.setContent("您好!!!您的注册已通过审核请注意查看!!!");
send.send();
}
public void send()
{
SimpleEmail email = new SimpleEmail();
email.setTLS(true);
email.setHostName(this.SMTPHost);
email.setAuthentication(this.user, this.passwd);
try
{
//to
email.addTo(this.to);
//from
email.setFrom(this.from);
//subject
email.setSubject(this.subject); //标题
//setting Charset
email.setCharset("UTF-8");
//content
email.setMsg(this.content);
//send
email.send();
System.out.println("发送完毕");
} catch (EmailException e) {
e.printStackTrace();
}
}
/**
* @return the sMTPHost
*/
public String getSMTPHost() {
return SMTPHost;
}
/**
* @param host the sMTPHost to set
*/
public void setSMTPHost(String host) {
SMTPHost = host;
}
/**
* @return the user
*/
public String getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(String user) {
this.user = user;
}
/**
* @return the passwd
*/
public String getPasswd() {
return passwd;
}
/**
* @param passwd the passwd to set
*/
public void setPasswd(String passwd) {
this.passwd = passwd;
}
/**
* @return the from
*/
public String getFrom() {
return from;
}
/**
* @param from the from to set
*/
public void setFrom(String from) {
this.from = from;
}
/**
* @return the to
*/
public String getTo() {
return to;
}
/**
* @param to the to to set
*/
public void setTo(String to) {
this.to = to;
}
/**
* @return the subject
*/
public String getSubject() {
return subject;
}
/**
* @param subject the subject to set
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
}
另外还有发送html的邮件和包含附件的邮件,在此只谈一下发纯文本的邮件
分享到:
相关推荐
java实例发送邮件 完整实例 详细说明
NULL 博文链接:https://mingruofei.iteye.com/blog/650347
一个及其简单的java发送邮件的实例 附带发邮件所用的jar包
好用的java发送邮件! 里面有相关注释! 好用的java发送邮件! 里面有相关注释!
Java发送邮件实例源代码,mail.jar activation.jar
java发送邮件2实例,内有java包。我原来是在myEcilpse上进行测试的,实例一是结合ssh框架使用,非常方便。实例二直接用servlet发送,稍微繁琐
教如何大家使用java发送邮件的实例,一看就会,也可以直接拿来用
本实例完整的实现了java发送邮件的功能,需要大家下载下来将自己的用户名和密码等替换,替换的文件在项目中的email.properties文件中,具体详情请参见博文:...
JAVA100例之实例45 使用SMTP协议发送邮件
一个独立的java 接收邮件实例,平时java发邮件这块应该说是资源都比较丰富了。但收这块比较少,所以就整合了一下相关资源。
使用mail.jar和activation-1.1.jar包发送邮件实例 使用mail.jar和activation-1.1.jar包发送邮件实例
JAVA 发送文本格和HTML格式并带图片链接的邮件实例,图片可以为附件可以为邮件正文显示,可以为图片添加超链接
通过java程序发邮件实例代码和说明,详细解说邮箱设置方法,附mailapi.jar和smtp.jar,傻瓜代码,可以复制进程序直接使用
邮件发送实例,注释详细,中文处理,html邮件
非常简单的Java邮件发送实例,一看就明白,对于初学很有帮助。
可以用163邮箱代理发送邮件的实例,包含所需的jar,很实用
用java发送邮件,内含有两个用java代码书的发送邮件的实例,分别实现发送文本格式的邮件和html格式的邮件
java发送邮件_实例.doc