Java邮件开发

本文介绍使用Java发送多种类型邮件的方法,包括简单的纯文本邮件、带有HTML格式的邮件及包含附件的邮件。通过示例代码展示了如何配置邮件服务器、设置认证信息及使用commons-email库简化邮件发送过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=red]要用到的jar包:mail.jar,activation.jar(jdk6不需要用此jar包),commons-email.jar。[/color]
[size=small][color=blue]1、发送时验证[/color][/size]

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SimpleMail{

public static void main(String[] args){

Properties properties=new Properties();
properties.setProperty("mail.smtp.auth","true");
properties.setProperty("mail.transport.protocol","smtp");

Session session=Session.getInstance(properties);
//设为debug模式可在控制台看到发送邮件的全过程
session.setDebug(true);

Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("superzangcao@sina.com"));
message.setSubject("simple java mail");
message.setText("hello,superzhang!this is a simple java mail");

Transport transport=session.getTransport();
//发送邮件时进行验证
transport.connect("smtp.sina.com", 25, "superzangcao","super");
transport.sendMessage(message,InternetAddress.parse("578806535@qq.com,customme@163.com,superzangcao@sina.com"));

}

}

[size=small][color=blue]2、连接时验证[/color][/size]

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.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class StaticTransport {

public static void main(String[] args) throws Exception{

Properties properties=new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.host", "smtp.sina.com");

//获取session实例时进行验证
//Authenticator是抽象类,此处有一个匿名类,该匿名类继承抽象类Authenticator,new的是匿名类,当然,你也可以单独写一个类继承Authenticator,重写它的getPasswordAuthentication()方法
Session session=Session.getInstance(properties,new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("superzangcao","super");
}
});
session.setDebug(true);

Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("superzangcao@sina.com"));
message.setRecipients(RecipientType.TO,InternetAddress.parse("578806535@qq.com,superzangcao@sina.com,beyondsuperzhang@gmail.com,customme@163.com"));
message.setSubject("static transport");
message.setContent("this email is transported by <font color='red'>static</font> transport","text/html;charset=utf-8;");

Transport.send(message);

}

}

[size=small][color=blue]3、带附件的邮件[/color][/size]

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class MultipartMail {

public static void main(String[] args)throws Exception{

Properties properties=new Properties();
properties.put("mail.smtp.auth", "auth");
properties.put("mail.transport.protocol", "smtp");

Session session=Session.getInstance(properties);
session.setDebug(true);

Message message=new MimeMessage(session);
message.setFrom(new InternetAddress("superzangcao@sina.com"));
message.setSubject("multipart mail");

Multipart multipart=new MimeMultipart("mixed");
message.setContent(multipart,"text/html;charset=utf-8;");

//邮件正文body
MimeBodyPart body=new MimeBodyPart();
Multipart bodyMultipart=new MimeMultipart("related");
BodyPart bodyPart=new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(new FileDataSource("D:/mail.bmp")));
bodyMultipart.addBodyPart(bodyPart);
body.setContent(bodyMultipart);
body.setContent("", "text/html;charset=utf-8");

//邮件附件attachment
MimeBodyPart attachment=new MimeBodyPart();
attachment.setDataHandler(new DataHandler(new FileDataSource("D:/ax_files.xml")));
attachment.setFileName(MimeUtility.encodeText("中文")+"xml");

multipart.addBodyPart(body);
multipart.addBodyPart(attachment);

Transport transport=session.getTransport();
transport.connect("smtp.sina.com",25, "superzangcao","super");
transport.sendMessage(message,InternetAddress.parse("beyondsuperzhang@gmail.com,beyondsuperzhang@qq.com"));

}

}


[size=small][color=blue]4、用commons-email发送简单邮件[/color][/size]

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

import org.apache.commons.mail.SimpleEmail;

public class SimpleMail {

public static void main(String[] args) throws Exception{

SimpleEmail email=new SimpleEmail();
email.setHostName("smtp.sina.com");
email.setAuthenticator(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("superzangcao","super");
}
});

email.setDebug(true);
email.setCharset("UTF-8");

email.setFrom("superzangcao@sina.com");
email.addTo("beyondsuperzhang@qq.com");
email.setSubject("你好!");
email.setMsg("hello,superzhang!很高兴认识你。");
email.send();

}

}


[color=blue][size=small]5、用commons-email发送带附件的邮件[/size][/color]

import javax.mail.BodyPart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;

public class MultiMail {

public static void main(String[] args) throws Exception {

MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.sina.com");
email.setAuthentication("superzangcao", "super");
email.setCharset("UTF-8");
email.setDebug(true);

email.setFrom("superzangcao@sina.com");
email.addTo("beyondsuperzhang@qq.com");
email.setSubject("带附件的邮件");

MimeMultipart body=new MimeMultipart();
BodyPart bodyPart=new MimeBodyPart();
bodyPart.setContent("<font color=red>你好,中文邮件也没问题</font>", "text/html;charset=utf-8");
body.addBodyPart(bodyPart);
email.addPart(body);

EmailAttachment attachment = new EmailAttachment();
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName(MimeUtility.encodeText("附件"));
attachment.setPath("D:\\Java邮件开发驱动包.rar");
email.attach(attachment);

email.send();

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值