发布网友 发布时间:2022-04-26 16:22
共3个回答
热心网友 时间:2023-10-14 05:34
package com.mail.test;
import java.util.Date;
import java.util.Properties;
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.sql.DataSource;
import sun.misc.BASEEncoder;
public class MailSender {
/**
* 发送单个邮件
* @throws Exception
*/
public void sendMail() throws Exception{
Properties props = new Properties();//创建属性对象
props.put("mail.smtp.host", getHost());//设置smtp服务器地址
props.put("mail.smtp.auth", "true");//设置服务器smtp需要验证
Session session = Session.getInstance(props, null);//创建新邮件并群发
//Session session = Session.getDefaultInstance(props);
//session.setDebug(true);
MimeMessage message = new MimeMessage(session);//创建过程对象
message.setFrom(new InternetAddress(getFromAddr()));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
message.setSubject(getTitle());//设置主题
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");//设置信件内容
multipart. addBodyPart(contentPart);
if(getAttachPath() != null && getAttachName() != null){
BodyPart attachmentPart= new MimeBodyPart();
// DataSource source = new FileDataSource(getAttachPath());
// attachmentPart.setDataHandler(new DataHandler(source));
BASEEncoder enc = new BASEEncoder();
attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, getUsername(), getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* 群发邮件
* @throws Exception
*/
public void sendMails() throws Exception{
Properties props = new Properties();//创建属性对象
props.put("mail.smtp.host", getHost());//设置smtp服务器地址
props.put("mail.smtp.auth", "true");//设置服务器smtp需要验证
Session session = Session.getInstance(props, null);//创建新邮件并群发
//Session session = Session.getDefaultInstance(props);
//session.setDebug(true);
MimeMessage message = new MimeMessage(session);//创建过程对象
message.setFrom(new InternetAddress(getFromAddr()));//设置发送邮件地址
message.setSentDate(new Date());//设置时间
InternetAddress[] address = new InternetAddress[this.getMutliTo().length]; //群发地址
for(int i = 0; i < this.getMutliTo().length; i++) { //循环发送
address[i] = new InternetAddress((this.getMutliTo())[i]);
}
message.setRecipients(Message.RecipientType.TO, address);
// message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
message.setSubject(getTitle());//设置主题
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");//设置信件内容
multipart. addBodyPart(contentPart);
if(getAttachPath() != null && getAttachName() != null){
BodyPart attachmentPart= new MimeBodyPart();
// DataSource source = new FileDataSource(getAttachPath());
// attachmentPart.setDataHandler(new DataHandler(source));
BASEEncoder enc = new BASEEncoder();
attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, getUsername(), getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
private String host = null; //邮件服务器
private String fromAddr = null; //发送邮件地址
private String toAddr = null; //接收邮件地址
private String username = null; //发送邮件用户名
private String password = null; //发送邮件密码
private String title = null; //邮件标题
private String attachPath = null; //邮件附件路径
private String attachName = null; //邮件附件名
private String sendtext = null; //邮件内容
private String[] MutliTo = null; //群发用户
public String[] getMutliTo() {
return MutliTo;
}
public void setMutliTo(String[] mutliTo) {
MutliTo = mutliTo;
}
public String getSendtext() {
return sendtext;
}
public void setSendtext(String sendtext) {
this.sendtext = sendtext;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFromAddr() {
return fromAddr;
}
public void setFromAddr(String fromAddr) {
this.fromAddr = fromAddr;
}
public String getToAddr() {
return toAddr;
}
public void setToAddr(String toAddr) {
this.toAddr = toAddr;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAttachPath() {
return attachPath;
}
public void setAttachPath(String attachPath) {
this.attachPath = attachPath;
}
public String getAttachName() {
return attachName;
}
public void setAttachName(String attachName) {
this.attachName = attachName;
}
}追问哥,javax.mail在java project工程中不能用啊,只能在web工程中用,我试过了的。
追答楼下都说了
热心网友 时间:2023-10-14 05:34
http://www.java-tips.org/java-se-tips/javax.swing/how-to-create-an-e-mail-client-in-java-3.html
这个是代码。然后你需要一个mail.jar。
http://www.java2s.com/Code/Jar/JKL/Downloadmailjar.htm 这个是地址。
下载jar之后吧mail.jar。导入工程就可以了,追问非常感谢提供了资料,但还是没有解决问题,就是javax在纯java工程中不能导入,只能在web工程中使用,我试过多遍了,虽然在myeclipse中可以点右键执行,不过一旦导出成jar包就不能执行,报错说找不到javax。
我已经有了mail.jar和activation.jar,并且之前在百度文档下载了资料、代码,在javaeye、csdn、百度、google等找了好多资料,但都大同小异,类似copy,只说了在web工程下如何收发邮件,却没有说在javaSE下如何收发邮件。
追答唉,把你的Lib文件夹和生成的jar放在一起就可以运行了。推荐使用netbeans,他会自动帮你把lib的包一起生成到一个dist文件里去,eclipse太落伍,只负责代码打包,第三方包更本不鸟,极其不人性的打包方式。
热心网友 时间:2023-10-14 05:35
javax.mail 可以用追问不能用,我试过好多遍了,不是在web工程下,是在java工程下。