java编写小型的局域网邮件发送
答案:2 悬赏:50 手机版
解决时间 2021-02-14 10:23
- 提问者网友:雨不眠的下
- 2021-02-13 17:57
java编写小型的局域网邮件发送
最佳答案
- 五星知识达人网友:痴妹与他
- 2021-02-13 18:50
我给你提供一个我在项目里面实际使用的代码.
这是我基于一个网上的代码自己修改封装过来的.
你可以参考一下
public class SimpleMail {
private static String encode = null;
static {
if ("\\".equals(File.separator)) {
encode = "GBK";
} else {
encode = "UTF-8";
}
}
public static boolean sendTextMail(MailInfo mailInfo) {
for (int i = 0; i < 3; i++) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties properties = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(properties,
authenticator);
if (mailInfo.isDebug()) {
sendMailSession.setDebug(true);
}
try {
Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息
Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址
mailMessage.setFrom(from);// 设置邮件消息的发送者
// Address to = new InternetAddress(mailInfo.getToAddress());//
// 创建邮件的接收者地址
// mailMessage.setRecipient(Message.RecipientType.TO, to);//
// 设置邮件消息的接收者
mailMessage.setRecipients(Message.RecipientType.TO,
wrapAddress(mailInfo.getToAddress()));
// InternetAddress ms = new
// InternetAddress(mailInfo.getMsAddress());
// mailMessage.setRecipient(Message.RecipientType.BCC, ms); //
// 密送人
mailMessage.setRecipients(Message.RecipientType.BCC,
wrapAddress(mailInfo.getMsAddress()));
mailMessage.setSubject(mailInfo.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
// mailMessage.setText(mailInfo.getContent());//设置邮件消息的主要内容
// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();// 创建一个包含附件内容的MimeBodyPart
// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);
mainPart.addBodyPart(messageBodyPart);
// 存在附件
String[] filePaths = mailInfo.getAttachFileNames();
if (filePaths != null && filePaths.length > 0) {
for (String filePath : filePaths) {
messageBodyPart = new MimeBodyPart();
File file = new File(filePath);
if (file.exists()) {// 附件存在磁盘中
FileDataSource fds = new FileDataSource(file);// 得到数据源
messageBodyPart
.setDataHandler(new DataHandler(fds));// 得到附件本身并至入BodyPart
messageBodyPart.setFileName("=?" + encode + "?B?"
+ file.getName());// 得到文件名同样至入BodyPart
mainPart.addBodyPart(messageBodyPart);
}
}
}
// 将MimeMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
Transport.send(mailMessage);// 发送邮件
return true;
} catch (Exception e) {
e.printStackTrace();
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return false;
}
private static Address [] wrapAddress(String[] adds) throws AddressException {
// String[] adds = mailInfo.getToAddress();
if(adds == null || adds.length == 0){
return null;
}
Address []to = new Address[adds.length];
for(int i = 0;i<adds.length;i++){
to[i]=new InternetAddress(adds[i]);
}
return to;
}
public static boolean sendHtmlMail(MailInfo mailInfo) {
for (int i = 0; i < 3; i++) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties properties = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(properties,
authenticator);
if (mailInfo.isDebug()) {
sendMailSession.setDebug(true);
}
try {
Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息
Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址
mailMessage.setFrom(from);// 设置邮件消息的发送者
// Address to = new InternetAddress(mailInfo.getToAddress());//
// 创建邮件的接收者地址
// mailMessage.setRecipient(Message.RecipientType.TO, to);//
// 设置邮件消息的接收者
mailMessage.setRecipients(Message.RecipientType.TO,
wrapAddress(mailInfo.getToAddress()));
// InternetAddress ms = new
// InternetAddress(mailInfo.getMsAddress());
// mailMessage.setRecipient(Message.RecipientType.BCC, ms); //
// 密送人
mailMessage.setRecipients(Message.RecipientType.BCC,
wrapAddress(mailInfo.getMsAddress()));
mailMessage.setSubject(mailInfo.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();// 创建一个包含HTML内容的MimeBodyPart
// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);
mainPart.addBodyPart(messageBodyPart);
// 存在附件
String[] filePaths = mailInfo.getAttachFileNames();
if (filePaths != null && filePaths.length > 0) {
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
for (String filePath : filePaths) {
messageBodyPart = new MimeBodyPart();
File file = new File(filePath);
if (file.exists()) {// 附件存在磁盘中
FileDataSource fds = new FileDataSource(file);// 得到数据源
messageBodyPart
.setDataHandler(new DataHandler(fds));// 得到附件本身并至入BodyPart
messageBodyPart.setFileName("=?" + encode + "?B?"
+ enc.encode(EmailFileNameConvert.changeFileName(file.getName()).getBytes())
+ "?=");// 得到文件名同样至入BodyPart
mainPart.addBodyPart(messageBodyPart);
}
}
}
// 将MimeMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
Transport.send(mailMessage);// 发送邮件
return true;
} catch (Exception e) {
e.printStackTrace();
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return false;
}
}
public class MailInfo implements Serializable{
private static final long serialVersionUID = -3937199642590071261L;
private String mailServerHost;// 服务器ip
private String mailServerPort;// 端口
private long timeout;// 超时时间
private String fromAddress;// 发送者的邮件地址
private String[] toAddress;// 邮件接收者地址
private String[] msAddress;// 密送地址
private String username;// 登录邮件发送服务器的用户名
private String password;// 登录邮件发送服务器的密码
private boolean validate = false;// 是否需要身份验证
private String subject;// 邮件主题
private String content;// 邮件内容
private String[] attachFileNames;// 附件的文件地址
private boolean debug;// 调试模式
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
p.put("mail.smtp.timeout", this.timeout);
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String[] getToAddress() {
return toAddress;
}
public void setToAddress(String[] toAddress) {
this.toAddress = toAddress;
}
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
public void setMsAddress(String[] msAddress) {
this.msAddress = msAddress;
}
public String[] getMsAddress() {
return msAddress;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public boolean isDebug() {
return debug;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public long getTimeout() {
return timeout;
}
}
public class MyAuthenticator extends Authenticator {
private String username = null;
private String password = null;
public MyAuthenticator() {
};
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
注意一下:
Myeclipse自带的JavaEE5.jar和java mail会发生冲突
找到ME下的javeee包
D:\MyEclipse 8.5\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\EE_5\javaee.jar
用rar等解压工具解开javaee.jar,删除里面的javax\mail文件夹(可以先备份javaee.jar)
也即,以后都不能使用javaee.jar里面的邮件api发送邮件了.
这是我基于一个网上的代码自己修改封装过来的.
你可以参考一下
public class SimpleMail {
private static String encode = null;
static {
if ("\\".equals(File.separator)) {
encode = "GBK";
} else {
encode = "UTF-8";
}
}
public static boolean sendTextMail(MailInfo mailInfo) {
for (int i = 0; i < 3; i++) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties properties = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(properties,
authenticator);
if (mailInfo.isDebug()) {
sendMailSession.setDebug(true);
}
try {
Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息
Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址
mailMessage.setFrom(from);// 设置邮件消息的发送者
// Address to = new InternetAddress(mailInfo.getToAddress());//
// 创建邮件的接收者地址
// mailMessage.setRecipient(Message.RecipientType.TO, to);//
// 设置邮件消息的接收者
mailMessage.setRecipients(Message.RecipientType.TO,
wrapAddress(mailInfo.getToAddress()));
// InternetAddress ms = new
// InternetAddress(mailInfo.getMsAddress());
// mailMessage.setRecipient(Message.RecipientType.BCC, ms); //
// 密送人
mailMessage.setRecipients(Message.RecipientType.BCC,
wrapAddress(mailInfo.getMsAddress()));
mailMessage.setSubject(mailInfo.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
// mailMessage.setText(mailInfo.getContent());//设置邮件消息的主要内容
// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();// 创建一个包含附件内容的MimeBodyPart
// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);
mainPart.addBodyPart(messageBodyPart);
// 存在附件
String[] filePaths = mailInfo.getAttachFileNames();
if (filePaths != null && filePaths.length > 0) {
for (String filePath : filePaths) {
messageBodyPart = new MimeBodyPart();
File file = new File(filePath);
if (file.exists()) {// 附件存在磁盘中
FileDataSource fds = new FileDataSource(file);// 得到数据源
messageBodyPart
.setDataHandler(new DataHandler(fds));// 得到附件本身并至入BodyPart
messageBodyPart.setFileName("=?" + encode + "?B?"
+ file.getName());// 得到文件名同样至入BodyPart
mainPart.addBodyPart(messageBodyPart);
}
}
}
// 将MimeMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
Transport.send(mailMessage);// 发送邮件
return true;
} catch (Exception e) {
e.printStackTrace();
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return false;
}
private static Address [] wrapAddress(String[] adds) throws AddressException {
// String[] adds = mailInfo.getToAddress();
if(adds == null || adds.length == 0){
return null;
}
Address []to = new Address[adds.length];
for(int i = 0;i<adds.length;i++){
to[i]=new InternetAddress(adds[i]);
}
return to;
}
public static boolean sendHtmlMail(MailInfo mailInfo) {
for (int i = 0; i < 3; i++) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties properties = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUsername(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(properties,
authenticator);
if (mailInfo.isDebug()) {
sendMailSession.setDebug(true);
}
try {
Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息
Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址
mailMessage.setFrom(from);// 设置邮件消息的发送者
// Address to = new InternetAddress(mailInfo.getToAddress());//
// 创建邮件的接收者地址
// mailMessage.setRecipient(Message.RecipientType.TO, to);//
// 设置邮件消息的接收者
mailMessage.setRecipients(Message.RecipientType.TO,
wrapAddress(mailInfo.getToAddress()));
// InternetAddress ms = new
// InternetAddress(mailInfo.getMsAddress());
// mailMessage.setRecipient(Message.RecipientType.BCC, ms); //
// 密送人
mailMessage.setRecipients(Message.RecipientType.BCC,
wrapAddress(mailInfo.getMsAddress()));
mailMessage.setSubject(mailInfo.getSubject());// 设置邮件消息的主题
mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间
// MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();// 创建一个包含HTML内容的MimeBodyPart
// 设置HTML内容
messageBodyPart.setContent(mailInfo.getContent(),
"text/html; charset=" + encode);
mainPart.addBodyPart(messageBodyPart);
// 存在附件
String[] filePaths = mailInfo.getAttachFileNames();
if (filePaths != null && filePaths.length > 0) {
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
for (String filePath : filePaths) {
messageBodyPart = new MimeBodyPart();
File file = new File(filePath);
if (file.exists()) {// 附件存在磁盘中
FileDataSource fds = new FileDataSource(file);// 得到数据源
messageBodyPart
.setDataHandler(new DataHandler(fds));// 得到附件本身并至入BodyPart
messageBodyPart.setFileName("=?" + encode + "?B?"
+ enc.encode(EmailFileNameConvert.changeFileName(file.getName()).getBytes())
+ "?=");// 得到文件名同样至入BodyPart
mainPart.addBodyPart(messageBodyPart);
}
}
}
// 将MimeMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
Transport.send(mailMessage);// 发送邮件
return true;
} catch (Exception e) {
e.printStackTrace();
try {
java.util.concurrent.TimeUnit.SECONDS.sleep(5);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return false;
}
}
public class MailInfo implements Serializable{
private static final long serialVersionUID = -3937199642590071261L;
private String mailServerHost;// 服务器ip
private String mailServerPort;// 端口
private long timeout;// 超时时间
private String fromAddress;// 发送者的邮件地址
private String[] toAddress;// 邮件接收者地址
private String[] msAddress;// 密送地址
private String username;// 登录邮件发送服务器的用户名
private String password;// 登录邮件发送服务器的密码
private boolean validate = false;// 是否需要身份验证
private String subject;// 邮件主题
private String content;// 邮件内容
private String[] attachFileNames;// 附件的文件地址
private boolean debug;// 调试模式
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
p.put("mail.smtp.timeout", this.timeout);
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String[] getToAddress() {
return toAddress;
}
public void setToAddress(String[] toAddress) {
this.toAddress = toAddress;
}
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
public void setMsAddress(String[] msAddress) {
this.msAddress = msAddress;
}
public String[] getMsAddress() {
return msAddress;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public boolean isDebug() {
return debug;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public long getTimeout() {
return timeout;
}
}
public class MyAuthenticator extends Authenticator {
private String username = null;
private String password = null;
public MyAuthenticator() {
};
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
注意一下:
Myeclipse自带的JavaEE5.jar和java mail会发生冲突
找到ME下的javeee包
D:\MyEclipse 8.5\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\EE_5\javaee.jar
用rar等解压工具解开javaee.jar,删除里面的javax\mail文件夹(可以先备份javaee.jar)
也即,以后都不能使用javaee.jar里面的邮件api发送邮件了.
全部回答
- 1楼网友:污到你湿
- 2021-02-13 19:19
内网是自己搭建的邮件服务器么?我以前用过apache的开源邮件服务器james,也是只需要设置props.setproperty("mail.host", "smtp.mymail.com");就可以了,至于smtp.mymail.com是可以配置在邮件服务器里面的,跟ip没关系的
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯