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发送及中文乱码处理。的更多相关文章

  1. 解决Java保存到数据库中文乱码问题,加useUnicode=true&characterEncoding=UTF-8

    Java保存到数据库中文乱码, 解决方法如下: 我们在连接MySQL数据库的时候一般都会在url后面添加useUnicode=true&characterEncoding=UTF-8,但是问什 ...

  2. 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)

    从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...

  3. [Java Web]Struts2解决中文乱码问题

    1.设置struts的字符编码,能够在struts.xml中添加下面代码: <constant name="struts.i18n.encoding" value=" ...

  4. java压缩zip文件中文乱码问题(转——作者:riching)

    本人遇到了同样的问题,用了以下方案,奇迹般的解决了.我很纳闷为什么,经理说:好读书,不求甚解,不要问为什么... 用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问 ...

  5. android客户端向服务器发送请求中文乱码的问

    android客户端向服务器发送请求的时候,并将参数保存到数据库时遇到了中文乱码的问题: 解决方法: url = "http://xxxx.com/Orders/saveorder.html ...

  6. java压缩zip文件中文乱码问题

    用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问题网上很多人给出了解决方法,两种:修改sun的源码:使用开源的类库org.apache.tools.zip.Zip ...

  7. 解决java图形界面label中文乱码

    第一:在你的具有main函数的类也即你应用运行的主类上点击右键,选择Run As中的Run Configurations,如下图:java,awt,中文方框,中文乱码第二,在Arguments标签下的 ...

  8. 通过Java创建XML(中文乱码已解决)

    package com.zyb.xml; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.Ou ...

  9. java压缩文件出现中文乱码问题

    在项目中需要压缩文件下载,做完了发现有中文乱码问题,终于明白了. 引入ant.jar包 import org.apache.tools.zip.ZipEntry;   import org.apach ...

随机推荐

  1. SSE求解向量大小

    float f=; __asm { mov esi, this ; vector u movups xmm0, [esi] ; first vector in xmm0 mulps xmm0, xmm ...

  2. 2013 ACM/ICPC Asia Regional Changsha Online–C (模拟)

    题目描述 略... 题解 注意控制精度即可....变量全部定义成double,结果round就行....妈蛋....被这题目恶心死了.... 代码: #include <iostream> ...

  3. Detect combined string

    写一个程序判断字符串A是否为其他两个字符串的组合,组合过程中其他两个字符串的相对顺序不能被破坏. 举例说明:abc和def可以组成字符串adebcf,但不能组成aefbcd,因为def的相对顺序已经被 ...

  4. C# WinForm多线程(三)---- Control.Invoke[转]

    开发中遇到更新Winform中控件假死问题,通过看了这篇文章了解了原理,感谢!此处标记起来,以备查看! 原文地址:http://www.cnblogs.com/joechen/archive/2009 ...

  5. 教程-EhLib70的安装方法

    1.下载EhLib 5.2包2.将Common文件下的文件全部复制到DELPHI7目录下.3.打开DELPHI7.0程序:打开DclEhLib70.dpk文件包点“编译”->“安装”打开EhLi ...

  6. 用C#来查看电脑硬件和系统信息

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用C#来查看电脑硬件和系统信息.

  7. java.util.Map按照key值合并的value的Collection 集合中。

    用java实现把多个map的内容合并的一个resultMap中 代码大致如下 /**  * @author Shalf  */ public class MapUtil { /** * 把partMa ...

  8. 机器人api(图灵机器人)

    图灵机器人,是基于自然语言解析的智能机器人,已经免费开放了机器人api接口,有需求的同学能够去试试. 图灵机器人官网:http://www.tuling123.com 图灵机器人体验:http://w ...

  9. Nginx代码调试——gdb工具

    参考网上的资料,写了一个configprint模块,其功能为打印输出location配置内容,并计数访问次数. 代码链接如下:https://github.com/PaulWeiHan/nginx_m ...

  10. QStyleFactory类参考

      QStyleFactory类创建QStyle对象.#include <QStyleFactory> 静态公有成员QStyle* create(const QString & k ...