命令行、代码发送电子邮件

命令行发送邮件脚本实例


#1.telnet 连接到smtp邮件服务器
telnet app-smtp-server.com 25

#2.打招呼
helo app-smtp-server.com

#3.设置邮件发起人邮箱
mail from:<mailuser@app-smtp-server.com>

#4.设置邮件接收人邮箱
rcpt to:<mailuser@app-smtp-server.com>

#5.开始输入数据内容
data

#6.设置邮件主题
subject:serverTest
#7.邮件内容 结束输入.号
test
.

java代码发送邮件实例

#Maven依赖
    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.7</version>
    </dependency>

1.MailSender.java

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class MailSender {

    private Session session = null;
    private Transport ts = null;

    public void init(){
        init(null);
    }
    public MailSender(){
    	
    }
    
    public MailSender(String host,String username,String password){
    	connect(host,username,password);
    }
    
    public void init(Properties prop){
        if (prop==null){
            prop = new Properties();
            prop.setProperty("mail.transport.protocol","smtp");
            prop.setProperty("mail.smtp.auth","true");
            prop.setProperty("mail.smtp.timeout","30000");
            prop.setProperty("mail.smtp.starttls.enable","true");
            prop.setProperty("mail.smtp.socketFactory.port","465");
            prop.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            prop.setProperty("mail.smtp.socketFactory.fallback","false");
        }
        session = Session.getInstance(prop);
    }
    public void connect(String host,String username,String password){
        if (session==null){
            init();
        }
        try {
            ts = session.getTransport();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
        try {
            ts.connect(host,username,password);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public void send(MailModel model){
        if (model==null||ts==null){ return;}
        MimeMessage mimeMessage = new MimeMessage(session);
        try {
            mimeMessage.setFrom(new InternetAddress(model.getFrom()));
            String recipients = model.getTo();
			List<InternetAddress> list = new ArrayList<InternetAddress>();
			String[] median = recipients.split(";");
			for (int i = 0; i < median.length; i++) {
				list.add(new InternetAddress(median[i]));
			}
			InternetAddress[] address = list.toArray(new InternetAddress[list.size()]);
            mimeMessage.setRecipients(Message.RecipientType.TO,address);
            mimeMessage.setSubject(model.getSubject());
            mimeMessage.setContent(model.getContent(),"text/html;charset=UTF-8");
            ts.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public void close(){
        try {
            if (ts!=null){
                ts.close();
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

2.MailModel.java

public class MailModel {
    private String from;
    private String to;
    private String subject;
    private String content;

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

3.TestMain.java

MailModel mailModel = new MailModel();
mailModel.setFrom(from);
mailModel.setTo(to);
mailModel.setSubject(subject);
mailModel.setContent(content);
MailSender mailSender = new MailSender();
try {
			
	Properties prop = new Properties();
	prop.setProperty("mail.smtp.host", host);
	prop.setProperty("mail.transport.protocol","smtp");
	mailSender.init(prop);
	mailSender.connect(host, null, null);
	mailSender.send(mailModel);
	log.info("---邮件发送完成.");
}catch (Exception e) {
	log.info("---邮件发送异常.");
}finally {
	try {
		mailSender.close();
	}catch(Exception e){
	}
}