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 ...
随机推荐
- 1到n数组,和为指定数所有序列问题
(1)方法一,背包问题解法 #include <iostream> using namespace std; #include <vector> #include <li ...
- vijosP1447 开关灯泡
vijosP1447 开关灯泡 链接:https://vijos.org/p/1447 [思路] 数学+高精度. 分析题目:题中有言,i时刻将其所有倍数的灯熄灭,由此得知一个数有多少个倍数就会被操作多 ...
- linux 开机自动挂载ntfs盘
1) 查看盘符UUID vellbibi@vell001:~$ sudo blkid [sudo] password for vellbibi: /dev/sda1: UUID="bce9e ...
- 活用maven使web.xml可以用maven变量
活用maven使web.xml可以用maven变量 废话不多说,直接上代码 <build> <finalName>${finalWarName}</finalName&g ...
- UVa1449 - Dominating Patterns(AC自动机)
题目大意 给定n个由小写字母组成的字符串和一个文本串T,你的任务是找出那些字符串在文本中出现的次数最多 题解 一个文本串,多个模式串,这刚好是AC自动机处理的问题 代码: #include <i ...
- POJ1423 - Big Number(Stirling公式)
题目大意 求N!有多少位 题解 用公式直接秒杀... 代码: #include<iostream> #include<cmath> using namespace std; # ...
- 从源码剖析一个Spark WordCount Job执行的全过程
原文地址:http://mzorro.me/post/55c85d06e40daa9d022f3cbd WordCount可以说是分布式数据处理框架的”Hello World”,我们可以以它为 ...
- 问题-Delphi编译时提示缺少delphi自己的单元文件
问题现象:在编译工程是,提示缺少DELPHI自己的很多单元. 问题原因:这可能是因为手动误删除,或是第三方控件安装时误删除DELPHI自己的目录引起的(如果说错了,希望高人指点). 问题处理: 方法一 ...
- 问题-[Delphi]无法设置断点
问题现象: 今天突然发现不知何时起DELPHI7除了.dpr文件外,其余各Unit都无法设置断点.即在设计状态下设置了断点,在运行后出现的无效断点!百思不得其解...后查了许多资料,花了两个小时才搞好 ...
- UIImage的使用
UIImage是IOS中层级比较高的一个用来加载和绘制图像的一个类,更底层的类还有CGImage,以及IOS5.0以后新增加的CIImage.今天我们主要聊一聊UIImage的三个属性: imageO ...