关于javamail用来发送邮件

本文介绍了一个使用Java实现的简单邮件发送程序。通过创建一个名为MailSender的类,文章详细展示了如何配置SMTP服务器、设置发件人及收件人信息,并附带了如何添加附件的具体步骤。此外,还提供了一个测试类来验证邮件发送功能。
摘要由CSDN通过智能技术生成

首先写一个类,用于得到与服务器连接的方式以及发件人收件人和邮件内容的信息:

package demo;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 sun.misc.BASE64Encoder;

public class MailSender{
    public void sendMail() throws Exception{
        Properties props = new Properties();
        props.put("mail.smtp.host", getHost());
        props.put("mail.smtp.auth", "true");
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(getFromAddr()));
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
        message.setSubject(getTitle());
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setText("这是我用Java程序发送的第一封邮件!呵呵!!!");//这里就是要发出去的邮件内容啦!
        multipart. addBodyPart(contentPart);
        if(getAttachPath() != null && getAttachName() != null){
            BodyPart attachmentPart= new MimeBodyPart();
            DataSource source = new FileDataSource(getAttachPath());
            attachmentPart.setDataHandler(new DataHandler(source));
            BASE64Encoder enc = new BASE64Encoder();
            attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
            multipart.addBodyPart(attachmentPart);
        }
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(host,getUsername(),getPassword());
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    private String host = null;
    private String fromAddr = null;
    private String toAddr = null;
    private String username = null;
    private String password = null;
    private String title = null;
    private String attachPath = null;
    private String attachName = null;
   
    public String getHost(){
        return host;
    }

    public void setHost(String host){
        this.host = host;
    }

    public String getFromAddr(){
        return fromAddr;
    }

    public void setFromAddr(String fromAddr){
        this.fromAddr = fromAddr;
    }

    public String getToAddr(){
        return toAddr;
    }

    public void setToAddr(String toAddr){
        this.toAddr = toAddr;
    }

    public String getUsername(){
        return username;
    }

    public void setUsername(String username){
        this.username = username;
    }

    public String getPassword(){
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTitle(){
        return title;
    }

    public void setTitle(String title){
        this.title = title;
    }

    public String getAttachPath(){
        return attachPath;
    }

    public void setAttachPath(String attachPath){
        this.attachPath = attachPath;
    }

    public String getAttachName(){
        return attachName;
    }

    public void setAttachName(String attachName){
        this.attachName = attachName;
    }
}

 

 

好了,基本上可以实现连接邮件服务器并发送了,接下来该写测试类来测试是否能够正确发送邮件,写个测试类:

package demo;

public class MailTest {
public static void main(String[] args){
        MailSender sender = new MailSender();
        sender.setHost("smtp.163.com"); //如果你要用什么邮箱就写什么邮箱的后缀名
        sender.setFromAddr("FromAddr@163.com");//发件人的邮箱地址
        sender.setToAddr("ToAddr@163.com");//收件人的邮箱地址
        sender.setUsername("邮箱用户名"); //发件人的用户名
        sender.setPassword("邮箱密码");//发件人的密码
        sender.setTitle("The first mail");//邮件的标题
        try {
        sender.sendMail();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}

那么也就实现发送邮件的功能了!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值