简单的邮件发送mail.jar

package com.future.portal.util;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class MailSender {
    final static Logger logger = Logger.getLogger(MailSender.class);
    /**
     * 发送简单邮件
     * @param str_from:发件人地址
     * @param str_to:收件人地址
     * @param str_title:邮件标题
     * @param str_content:邮件正文
     * @param emailPwd:邮件密码
     */
    @SuppressWarnings("finally")
    public static void send(String str_to, String str_title,String str_content){
         boolean flog=false;
        logger.info("sending... To: " + str_to + " Mail Title: " + str_title + " fileAttachment: " + str_content);
            Properties props = new Properties();
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

            try {
                props.load(classLoader.getResourceAsStream("mail.properties"));   //使用property读取文件
            } catch (IOException e) {
                e.printStackTrace();
            }
            String str_from = props.getProperty("mail.from");
            String emailPwd = props.getProperty("mail.password");
            final String mailServer_163 = props.getProperty("mail.smtp.163.mailServer");
            final String mailServer_qq = props.getProperty("mail.smtp.qq.mailServer");
            final String mailServer_Sina = props.getProperty("mail.smtp.sina.mailServer");
            String mailServer = "";
            String emailType = Tools.subString(str_from, "@"); //截取@前
            String eName = Tools.subberString(str_from,"@");
            if (emailType.trim() != "" && emailType.equals("qq.com")) {
                mailServer = mailServer_qq;
            }
            if (emailType.trim() != "" && emailType.equals("163.com")) {
                mailServer = mailServer_163;
            }
            if (emailType.trim() != "" && emailType.equals("sina.com.cn")) {
                mailServer = mailServer_Sina;
            }
            if (emailType.trim() != "" && emailType.equals("sina.com")) {
                mailServer = mailServer_Sina;
            }
           // 存储发送邮件服务器的信息
            props.put("mail.smtp.host", mailServer);
            // 同时通过验证
            props.put("mail.smtp.auth", "true");
            // 根据属性新建一个邮件会话
            Session s = Session.getInstance(props);
            s.setDebug(true); // 有他会打印一些调试信息。
            // 由邮件会话新建一个消息对象
            MimeMessage message = new MimeMessage(s);
            // 设置邮件
            InternetAddress from = null;
            try {
                from = new InternetAddress(str_from);
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // [email protected]
            try {
                message.setFrom(from);
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // 设置发件人的地址
            // //设置收件人,并设置其接收类型为TO
            InternetAddress to = null;
            try {
                to = new InternetAddress(str_to);
            } catch (AddressException e) {
                e.printStackTrace();
            }
            try {
                message.setRecipient(Message.RecipientType.TO, to);
                // 设置标题
                message.setSubject(str_title); // java学习
                // 设置信件内容
                // message.setText(str_content); //发送文本邮件 //你好吗?
                message.setContent(str_content, "text/html;charset=gbk"); // 发送HTML邮件
                // 设置发信时间
                message.setSentDate(new Date());
                // 存储邮件信息
                message.saveChanges();
            } catch (MessagingException e) {
                e.printStackTrace();
            }

            // 发送邮件
            Transport transport = null;
            try {
                transport = s.getTransport("smtp");
            } catch (NoSuchProviderException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
            try {
                transport.connect(mailServer, eName, emailPwd) ;
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            // 发送邮件,其中第二个参数是所有已设好的收件人地址
            try {
                transport.sendMessage(message, message.getAllRecipients());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            try {
                transport.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }   

    }

    /**
     * 异步发送邮件的方法*/
    public static void sendMailByAsynchronous(final String str_to, final String str_title, final String str_content) throws Exception{
        new Thread(new Runnable() {
            public void run() {
                try {
                    MailSender.send(str_to, str_title, str_content);
                } catch (Exception ex) {
                    logger.error("mail sender error To: " + str_to
                            + " Mail Title: " + str_title + " fileAttachment: "
                            + str_content, ex);
                }
            }
        }).start();
    }

}
mail.smtp.163.mailServer=smtp.163.com

mail.smtp.qq.mailServer=smtp.qq.com

mail.smtp.sina.mailServer=smtp.sina.com.cn

mail.username=asd

mail.password=123456

mail.from[email protected]
时间: 2024-12-11 10:40:48

简单的邮件发送mail.jar的相关文章

第四周作业 简单地邮件发送实现

先说发送邮箱用到的三大类: 创建和解析邮件内容的API :Message类是创建和解析邮件的核心API,它的实例对象代表一封电子邮件. 发送邮件的API:Transport类是发送邮件的核心API类,它的实例对象代表实现了某个邮件发送协议的邮件发送对象,例如SMTP协议. 接收邮件的API:Store类是接收邮件的核心API类,它的实例对象代表实现了某个邮件接收协议的邮件接收对象,例如POP3协议. 还有一个提供会话信息的类Session. 简单地邮件发送的过程: 相关的协议说明:见 smtp:

C#实现简单的邮件发送功能

唉,最近要做一个项目,里面需要实现邮件发送功能.在网络上也找一些看,自己也随便写下.也当做是给自己复习下吧,如有不对之处还请大家指出.谢谢! 首先我是创建一个实体对象Model (EmailParameterSet) : 1 public class EmailParameterSet 2 { 3 /// <summary> 4 /// 收件人的邮件地址 5 /// </summary> 6 public string ConsigneeAddress { get; set; }

.Net实现简单的邮件发送

使用winform写了一个小程序,实现简单的邮件发送功能,部分代码如下: //异步发送邮件: 1 //发送按钮 2 private void btn_Send_Click(object sender, EventArgs e) 3 { 4 //创建MailMessage对象 5 MailAddress from = new MailAddress("[email protected]"); 6 MailAddress to = new MailAddress("[email 

php 简单实现 邮件发送

初中目的是为了学习 WEBSOCKET ,所以写了一个简单发送邮件的例子,为后面打下基础.代码如下: class Mail {     private $host;     private $port;     private $user;     private $password;     private $socket;         public function __construct()    { //所使用邮件服务器        $this->host = 'smtp.outl

C#,简单的邮件发送功能,邮件辅助类

这个帮助类只能发送简单的基本邮件,只能发送给多个用户,单一内容.不具有抄送功能,附件添加功能.功能代码如下, private void button3_Click(object sender, EventArgs e) { int result = SendEmail("[email protected]", "123456", new string[] { "[email protected]" }, "测试标题", &qu

简单的邮件发送程序

using System;using System.Net;using System.Net.Mail;using System.Text; namespace SendMailDemo{ class Program { static void Main(string[] args) { SmtpClient client = new SmtpClient("smtp.goodix.com"); MailAddress from = new MailAddress("[ema

发送邮件(遵循smtp协议即简单的邮件发送协议)

HandleSendEmail.aspx逻辑 protected void Page_Load(object sender,EventArgs e) { foreach(var item in Request.Form.Keys) { if(item.ToString().StartsWidth("txtMail")) { string mail=Request.Form[item.ToString()]; //设置smtp服务器和端口 SmtpClient smtpclient=ne

[vb.net]最简单的邮件发送

Imports Microsoft.Office.Interop.Outlook Private Sub sendMail() Dim outObj As New Application Dim item As MailItem item = outObj.CreateItem(0) '设定收件人地址 item.To = "[email protected]" '设定邮件主题 item.Subject = "test" '设定邮件内容 item.Body = &qu

配置Mail邮件发送

tail -5 /etc/mail.rc  #加上这几行,代表用我这个账号登陆邮件去发送消息 set [email protected] set smtp=smtp.126.com set [email protected] set smtp-auth-password=123456 set smtp-auth=login 发送邮件命令 1.使用管道进行邮件发送 echo "hello" | mail -s "Hello from mzone.cc by pipe"