发送邮件的一种写法 (手动发送)

本文提供了一个使用Java发送带有HTML格式内容的电子邮件的示例代码。通过配置SMTP服务器及认证信息,实现从指定邮箱发送邮件到目标邮箱的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
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.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;


/**
 *
 * @author wgh
 *  后台 加盟Chitone>>应聘管理  邮件回复
 *
 */
public class ReplyEmail{

    private static String username = "chitone@job5156.com";
    private static String password = "wgh1***842****3";

    public static void main(String args[])throws Exception{
       
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.job5156.com");
        String from = "chitone@job5156.com";    //job@job5156.com
        String to = "w**huan@126.com";       
       
        //获取邮件会话对象
        Session session = Session.getDefaultInstance(props,
                new SmtpAuthenticator(username,password));
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));                    //设置发信人
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));                        //设置收信人
        msg.setSentDate(new Date());                                           
        msg.setSubject("感谢您申请 Chitone xx公司 的 销售代表 职位");
        Multipart mp = new MimeMultipart("related");            //related 意味着可以发送html格式的邮件
        BodyPart bodyPart = new MimeBodyPart();                    //邮件正文
        bodyPart.setDataHandler(new DataHandler("~~~hello~~~","text/html;charset=GBK"));//网页格式
        mp.addBodyPart(bodyPart);
        msg.setContent(mp);
        Transport.send(msg);                                    //发送邮件
       
        //更改应聘者邮件回复状态
    }
}
   
    /**
     * Smtp 认证
     */
    class SmtpAuthenticator extends Authenticator{
        String username = null;
        String password = null;
       
        //SMTP身份验证
        public SmtpAuthenticator(String username,String password){
            this.username = username;
            this.password = password;
        }
        public PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(this.username,this.password);
        }
    }
   
    class ByteArrayDataSource implements DataSource{
        private final String contentType;
        private final byte[] buf;
        private final int len;
       
        public ByteArrayDataSource(byte[] buf,String contentType){
            this(buf,buf.length,contentType);
        }
       
        public ByteArrayDataSource(byte[] buf,int length,String contentType){
            this.buf = buf;  
            this.len = length;  
            this.contentType = contentType;  
        }  
     
        public String getContentType() {  
            if (contentType == null)  
                return "application/octet-stream";  
            return contentType;  
        }  
     
        public InputStream getInputStream() {  
            return new ByteArrayInputStream(buf, 0, len);  
        }  
     
        public String getName() {  
            return null;  
        }  
     
        public OutputStream getOutputStream() {  
            throw new UnsupportedOperationException();  
        }  
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值