java Email发送及中文乱码处理。
public class mail
{ private String pop3Server="";
private String smtpServer=""; private String srcMailAddr="";
private String user="";
private String password=""; public mail()
{
}
public boolean getConfig(){ this.pop3Server="pop.163.com";
this.smtpServer="smtp.163.com"; this.srcMailAddr="cup209@163.com";
this.user="cup209";
this.password="******"; return true;
}
public boolean checkMailAddr(String mailAddr){
if(null==mailAddr){
return false;
}
return mailAddr.matches("^[a-z0-9A-Z_]{1,}@[a-z0-9A-Z_]{1,}$");
}
public boolean send(String dstMailAddr,String subject,String content){ if(checkMailAddr(dstMailAddr)){
System.out.print("send mail fail:dst mail addr error");
return false;
}
try
{
this.getConfig();
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host", this.smtpServer); //smtp主机名。
props.put("mail.smtp.user",this.srcMailAddr); //发送方邮件地址。
props.put("mail.smtp.password",this.password); //邮件密码。
PopupAuthenticator popA=new PopupAuthenticator();//邮件安全认证。
PasswordAuthentication pop = popA.performCheck(this.user,this.password); //填写用户名及密码
sendMailSession = Session.getInstance(props, popA);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(this.srcMailAddr));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(dstMailAddr)); //接收方邮件地址
newMessage.setSubject(Tool.convertGBK2UTF8(subject));
newMessage.setSentDate(new Date());
String mailContent=content;
try{
newMessage.setText(Tool.gbToUtf8(mailContent)); //邮件正文
}catch(Exception e){}
//newMessage.setContent(content,"text/html;charset=GBK");
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage); }
catch (MessagingException ex)
{
ex.printStackTrace();
return false;
}
return true;
} public static void main(String[] args)
{
mail sml = new mail();
sml.send("cup209@163.com","测试邮件","<a href='http://www.163.com'>测试邮件内容</a>");
} public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
} }
}
java Email发送及中文乱码处理。的更多相关文章
- 解决Java保存到数据库中文乱码问题,加useUnicode=true&characterEncoding=UTF-8
Java保存到数据库中文乱码, 解决方法如下: 我们在连接MySQL数据库的时候一般都会在url后面添加useUnicode=true&characterEncoding=UTF-8,但是问什 ...
- 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)
从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...
- [Java Web]Struts2解决中文乱码问题
1.设置struts的字符编码,能够在struts.xml中添加下面代码: <constant name="struts.i18n.encoding" value=" ...
- java压缩zip文件中文乱码问题(转——作者:riching)
本人遇到了同样的问题,用了以下方案,奇迹般的解决了.我很纳闷为什么,经理说:好读书,不求甚解,不要问为什么... 用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问 ...
- android客户端向服务器发送请求中文乱码的问
android客户端向服务器发送请求的时候,并将参数保存到数据库时遇到了中文乱码的问题: 解决方法: url = "http://xxxx.com/Orders/saveorder.html ...
- java压缩zip文件中文乱码问题
用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问题网上很多人给出了解决方法,两种:修改sun的源码:使用开源的类库org.apache.tools.zip.Zip ...
- 解决java图形界面label中文乱码
第一:在你的具有main函数的类也即你应用运行的主类上点击右键,选择Run As中的Run Configurations,如下图:java,awt,中文方框,中文乱码第二,在Arguments标签下的 ...
- 通过Java创建XML(中文乱码已解决)
package com.zyb.xml; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.Ou ...
- java压缩文件出现中文乱码问题
在项目中需要压缩文件下载,做完了发现有中文乱码问题,终于明白了. 引入ant.jar包 import org.apache.tools.zip.ZipEntry; import org.apach ...
随机推荐
- 高并发Web服务的演变:节约系统内存和CPU
一.越来越多的并发连接数 现在的Web系统面对的并发连接数在近几年呈现指数增长,高并发成为了一种常态,给Web系统带来不小的挑战.以最简单粗暴的方式解决,就是增加Web系统的机器和升级硬件配置.虽然现 ...
- 开始使用storm
开始使用storm 本章将讲述如何安装.部署.启动和停止 Storm 集群. Storm 的安装比较简单,但在安装 Storm 之前需要做好充足的准备,本章将介绍安装的整个流程.在官网上可以下载到S ...
- poj 1144 Network【双连通分量求割点总数】
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11042 Accepted: 5100 Descript ...
- javascript-智能社-笔记
JavaScript是什么 JavaScript就是修改样式 编写JS的流程 布局:HTML+CSS 属性:确定要修改哪些属性 事件:确定用户做哪些操作(产品设计) 编写JS:在事件中,用JS来修改页 ...
- SMB带宽限制
1.安装SMB带宽限制功能 Add-WindowsFeature FS-SMBBW 2.通过PowerShell命令限制SMB带宽 #限制非Hyper-V over SMB or Live Migra ...
- Highcharts简介
最近要做一个油田油压或温度数据的监控软件,数据会秒级写到数据库中,界面上需要动态展示数据跟随时间变化. 在网上找了很多js插件,希望能够即时高效的展示数据,最终确定了使用Highcharts插件. H ...
- android 无线模式下使用ADB调试
改动系统system权限 1. cp sud /system/xbin/sud chmod 755 /system/xbin/sud // note: su chmod 成755 2. cp su / ...
- 六款值得推荐的android(安卓)开源框架简介【转】
http://my.oschina.net/u/1244156/blog/380647 1.volley 项目地址 https://github.com/smanikandan14/Volley-de ...
- java对空格的处理
public static void main(String[] args) { // TODO Auto-generated method stub String a = " 这个前面有两 ...
- [Javascript] Implement zip function
1. Use a for loop to traverse the videos and bookmarks array at the same time. For each video and bo ...