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 ...
随机推荐
- editpuls查找替换通配符
1 \t Tab character. tab符号 2 \n New line. 新的一行(换行符) 3 . Matches any ...
- T-SQL XQuery (XML路径查询) (转)http://blog.csdn.net/Beirut/article/details/8150116
/* T-SQL 支持用于查询 XML 数据类型的 XQuery 语言的子集. XQuery 基于现有的 XPath 查询语言,并支持更好的迭代.更好的排序结果以及构造必需的 XML 的功能. 在前面 ...
- sensor的skipping and binning 模式
关于sensor的缩放存在两种模式 binning mode 和skip mode skipping mode 就是把想要的数据采集上来 把其余的数据扔掉 如下所示 column skip 2 row ...
- 配置 Web Deploy 的步骤 -摘自网络
今天的文章里,我会介绍Microsoft Web Deploy—一个采用全面的发布和部署机制的免费服务器技术.Web Deploy不仅仅让你发布文件—还可以部署数据库结构/数据,运行变更的数据库脚本, ...
- 推送通知/传感器/UIDynamic仿真(推送通知已适配iOS10)
推送通知/传感器/UIDynamic 一.推送通知 1.推送通知简介 什么是推送通知 此处的推送通知与NSNotification没有任何关系 可以理解为,向用户推送一条信息来通知用户某件事情 作用: ...
- Win7与虚拟机VMware下运行的Ubuntu共享文件夹
安装VMware Tools,在VMware面板上选择“虚拟机-重新安装VMware tools…”,如下图所示: 在这里VMware虚拟了一个光盘镜像,我们需要把这个镜像挂载到本机的/mnt目录下面 ...
- Xcode6为什么干掉pch(Precompile Prefix Header)&如何添加pch文件
转载: http://blog.csdn.net/iosdevtip/article/details/40918353 一直在用xcode6开发,但项目都是在xcode5上创建的,所以一直没注意到, ...
- Python+Django+SAE系列教程17-----authauth (认证与授权)系统1
通过session,我们能够在多次浏览器请求中保持数据,接下来的部分就是用session来处理用户登录了. 当然,不能仅凭用户的一面之词,我们就相信,所以我们须要认证. 当然了,Django 也提供了 ...
- IOPS=(Queue Depth)/(IO latency)
IO 延迟:存储设备的IO延迟 Queue Depth:磁盘控制器所发出的批量指令的最大条数 IOPS:磁盘设备每秒的IO 三者之间的关系:IOPS=(Queue Depth)/(IO latency ...
- I'm back
亲爱的博友们, 请忽略这一条, 这只是我个人的一个记录.