java实现发邮件功能

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.sun.mail.util.MailSSLSocketFactory;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
public class TestEmailSend {
public static void main(String[] args) {
String email_filepath =“D:\order1622515607844_823278.pdf”;
//sendEmail(xxxx@xxx.com", “测试供应商”, “测试供应商”, “测试供应商”, email_filepath);
sendEmail(“xxxx@xxxx.cn”, “测试供应商”, “测试供应商”, “测试供应商”, email_filepath);
// sendEmail(“xxxx@qq.com”, “测试供应商”, “测试供应商”, “测试供应商”, email_filepath);
}
public static void sendEmail(String email_addr,String email_receiver_name,String email_title,String email_content,String email_filepath){
System.out.println(“sendEmail__start_____”);
System.out.println(“email_addr_____”+email_addr);
System.out.println(“email_receiver_name_____”+email_receiver_name);
System.out.println(“email_title_____”+email_title);
System.out.println(“email_content_____”+email_content);
System.out.println(“email_filepath_____”+email_filepath);
if(!"".equals(email_addr)) {
try {
send(email_addr,email_title,email_content,email_filepath);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(“sendEmail__end_____”);
}

private static void send(String emailAddress,String title,String cons,String filePath) throws UnsupportedEncodingException {
    System.out.println("send_filepath_____"+filePath);
    Properties prop = new Properties();
    //协议
    prop.setProperty("mail.transport.protocol", "smtp");
    //服务器
    prop.setProperty("mail.smtp.host", "mail.xxx.com");
    //端口
    prop.setProperty("mail.smtp.port", "587");
    //使用smtp身份验证
    prop.setProperty("mail.smtp.auth", "true");
    //使用SSL,企业邮箱必需!
    //开启安全协议
    MailSSLSocketFactory sf = null;
    try {
        sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
    } catch (GeneralSecurityException e1) {
        e1.printStackTrace();
    }
    prop.put("mail.smtp.ssl.enable", "false");
    prop.put("mail.smtp.ssl.socketFactory", sf);
    //
    //获取Session对象
    Session s = Session.getDefaultInstance(prop,new Authenticator() {
        //此访求返回用户和密码的对象
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            PasswordAuthentication pa = new PasswordAuthentication("srinf@xxx.com", "密码");
            return pa;
        }
    });
    //设置session的调试模式,发布时取消
    s.setDebug(true);
    MimeMessage mimeMessage = new MimeMessage(s);
    try {
        mimeMessage.setFrom(new InternetAddress("srinf@xxx.com","srinf@xxx.com"));
        mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
        //设置主题
        mimeMessage.setSubject(title);
        mimeMessage.setSentDate(new Date());
        //设置内容
        mimeMessage.setText(cons);
        if(!"".equals(filePath)){
            // 创建消息部分
            BodyPart messageBodyPart = new MimeBodyPart();
            // 消息
            messageBodyPart.setText(cons);
            // 创建多重消息
            Multipart multipart = new MimeMultipart();
            // 设置文本消息部分
            multipart.addBodyPart(messageBodyPart);
            // 附件部分
            messageBodyPart = new MimeBodyPart();
            //设置要发送附件的文件路径
            File file = new File(filePath);
            DataSource source = new FileDataSource(filePath);
            System.out.println("send_fileName_____"+file.getName());
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(file.getName());
            //处理附件名称中文(附带文件路径)乱码问题
            messageBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
            multipart.addBodyPart(messageBodyPart);
            // 发送完整消息
            mimeMessage.setContent(multipart);
        }
        mimeMessage.saveChanges();
        //发送
        Transport.send(mimeMessage);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    System.out.println(emailAddress+"----邮件发送完成");
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值