使用java向邮箱发送邮件
这是我很早之前写的一个工具类,最近在整理自己所学的东西,无意中找到了,就拿出来与大家分享,代码如下:
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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; import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart; import org.apache.struts.util.MessageResources; import com.qq.connect.javabeans.tenpay.Address; //import com.sun.istack.internal.ByteArrayDataSource;
public class SendMails {
public int sendMail(String emailTitle, String content, String emailTo,MessageResources message) throws Exception {
System.out.println(smtpServer1);
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put( "mail.transport.protocol", "smpt");
props.put("mail.smtp.host", smtpServer1);
// 获得邮件会话对象
Session session = Session.getInstance(props,new SmtpAuthenticator1(fromMailAddress1, password1));
/** *************************************************** */
// 创建MIME邮件对象
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromMailAddress1));// 发件人
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));// 收件人
mimeMessage.setSubject(emailTitle);
mimeMessage.setSentDate(new Date());// 发送日期
Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
/** *************************************************** */
BodyPart bodyPart = new MimeBodyPart();// 正文
StringBuffer content = new StringBuffer();
bodyPart.setDataHandler(new DataHandler(content.toString(),"text/html;charset=GBK"));// 网页格式
/** *************************************************** */
// BodyPart attachBodyPart = new MimeBodyPart();// 普通附件
// FileDataSource fds = new FileDataSource("c:/boot.ini");
// attachBodyPart.setDataHandler(new DataHandler(fds));
// attachBodyPart.setFileName("=?GBK?B?"+ new sun.misc.BASE64Encoder().encode(fds.getName().getBytes()) + "?=");// 解决附件名中文乱码
// mp.addBodyPart(attachBodyPart);
/** *************************************************** */
// MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件图标
// byte[] bytes = readFile("E:/webcms/webcms/WebContent/images/bjx.jpg");
// ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,"application/octet-stream");
// imgBodyPart.setDataHandler(new DataHandler(fileds));
// imgBodyPart.setFileName("button.gif");
// imgBodyPart.setHeader("Content-ID", "<IMG1></IMG1>");// 在html中使用该图片方法src="cid:IMG1"
// mp.addBodyPart(imgBodyPart);
/** *************************************************** */
mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);//设置邮件内容对象
try {
Transport.send(mimeMessage);//发送邮件
// Transport transport=session.getTransport();
// transport.send(mimeMessage);
} catch (Exception e) {
// e.printStackTrace();
if(e.getMessage().contains("550 User not found")){
return 2;
}else{
return 3;
}
}
//system.out.println("SendEmail OK!!");
return 0;
}
public void setUsername1(String username1) {
this.username1 = username1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public void setSmtpServer1(String smtpServer1) {
this.smtpServer1 = smtpServer1;
} public void setFromMailAddress1(String fromMailAddress1) {
this.fromMailAddress1 = fromMailAddress1;
}
public static byte[] readFile(String file) {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
int bytesRead;
byte buffer[] = new byte[1024 * 1024];
while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
Arrays.fill(buffer, (byte) 0);
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bos.toByteArray();
}
}
/**
* Smtp认证
*/
class SmtpAuthenticator1 extends Authenticator {
String username = null;
String password = null; // SMTP身份验证
public SmtpAuthenticator1(String username, String password) {
this.username = username;
this.password = password;
System.out.println(username+"::::::"+password);
} public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
} }
使用java向邮箱发送邮件的更多相关文章
- java实现邮箱发送邮件功能
邮箱验证是一个很常见的功能了,基本上每个网站都会用的到,java也有专门的jar来处理邮件发送等服务,这里只是简单的实现一下发送邮件的功能,具体jar包就不再提供了,我会把所有需要引用的包都贴出来,方 ...
- java实现邮箱发送邮件
第一步:封装发件人账号密码 import javax.mail.Authenticator;import javax.mail.PasswordAuthentication; /** * 发件人账号密 ...
- Java实现网易企业邮箱发送邮件
最近项目需要用网易企业邮箱发送邮件,特意来将实现过程记录一下: maven导入jar包 <!-- javax.mai 核心包 --> <dependency> <grou ...
- 基于java mail实现简单的QQ邮箱发送邮件
刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习. -----------------------------第一步 开启SMTP服务 在 QQ 邮箱里的 设置->账户里开启 S ...
- 使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.MessagingException: Exception reading response
使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.Messagin ...
- Java实现邮箱验证
Java实现邮箱验证 JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.我们可以基于JavaMail开 ...
- 基于JavaMail向邮箱发送邮件
参考:http://blog.csdn.net/ghsau/article/details/17839983 http://blog.csdn.net/never_cxb/article/detail ...
- Java注册帐号邮箱激活验证实现
Java注册帐号邮箱激活验证实现 1.需要加入发送邮件的mail.jar: http://www.oracle.com/technetwork/java/javamail/index-138643.h ...
- 通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)
package server; import java.io.*; import java.net.*; import java.sql.Time; import java.util.Scanner; ...
随机推荐
- ZBrush通过显示与隐藏得到子物体
在ZBrush®中得到子物体的方法有很多,本文将为大家介绍一种新的创建子物体的方法,通过显示和隐藏得到子物. ZBrush 4R8中文版下载:http://wm.makeding.com/iclk/? ...
- javscript中变量的作用域和提升
示例: var a = 1; function foo() { if (!a) { var a = 10; } alert(a); }; foo(); 上面这段代码在运行时会产生 ...
- vector的resize与reserve的区别
- [置顶]
智能家居开源项目 The open Home Automation Bus (openHAB)
================================================================================ 2014-05-19 论文的事情太多, ...
- Vue学习之路第一篇(学习准备)
1.开发工具的选择 这个和个人的开发习惯有关,并不做强求,厉害的话用记事本也可以.但是我还是建议用人气比较高的编辑工具,毕竟功能比较全面,开发起来效率比较高. 我之前写前端一直用的是sublimete ...
- Linux下实时查看GPU状况
1. 显示当前GPU使用情况 Nvidia自带了一个nvidia-smi的命令行工具,会显示显存使用情况: $ nvidia-smi 输出如下: 2. 周期性输出GPU使用情况 但是有时我们希望不仅知 ...
- Django REST Framework 数码宝贝 - 3步进化 - 混合类 -->
读了我这篇博客, 你会刷新对面对对象的认知, 之前的面对对象都是LJ~~~ 表结构 class Publisher(models.Model): name = models.CharField(max ...
- Maven学习总结(23)——Maven常用命令介绍
1.生成eclipse项目:mvn eclipse:eclipse 2.清除eclipse的一些系统设置:mvn eclipse:clean 3.mvn tomcat:run 在tomcat里面运行 ...
- POJ 2773
不经意看见dis后的“mod”一词后,瞬间有了思路,点进去看,却发现别人想的和我的不一样——! 我是这样想的,利用的是剩余系+欧几里德带余除法的性质. 若两者GCD=1,则必有除数和余数GCD=1.于 ...
- java语言MySQL批处理
本质来讲就是使用Statement和PreStatement的addBatch()方法 代码 import java.sql.*; public class GetConnection{ public ...