| 協議 | 全稱 | 用途 | 預設埠 |
|---|---|---|---|
| SMTP | Simple Mail Transfer Protocol | 發送郵件 | 25 / 465(SSL) / 587(TLS) |
| POP3 | Post Office Protocol v3 | 接收郵件(下載到本地) | 110 / 995(SSL) |
| IMAP | Internet Message Access Protocol | 接收郵件(伺服器同步) | 143 / 993(SSL) |
apache-james-2.3.2.zip 解壓bin/run.bat 啟動(預設埠 4555 管理、25 SMTP)apps/james/SAR-INF/config.xml 設定域名、用戶庫等telnet localhost 4555
# 登入管理員
root
root
# 新增用戶
adduser user1 password1
adduser user2 password2
user1,密碼:password1
import javax.mail.*;
public class EmailAuthenticator extends Authenticator {
private String username, password;
public EmailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class MailSender {
private String host, from, to, subject, content, username, password;
public MailSender(String host, String username, String password,
String from, String to, String subject, String content) {
this.host = host; this.username = username; this.password = password;
this.from = from; this.to = to; this.subject = subject; this.content = content;
}
public void send() {
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props, new EmailAuthenticator(username, password));
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject, "UTF-8");
msg.setContent(content, "text/html;charset=UTF-8");
msg.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println("郵件發送成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class TestMail {
public static void main(String[] args) {
new MailSender(
"192.168.17.176", "user1", "password1",
"user1@mail.com", "user2@mail.com",
"會議通知",
"XX你好!請於今天下午16:00 準時到B01會議室召開技術討論會。"
).send();
}
}

import org.springframework.mail.javamail.*;
public class MailWithAttachment {
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; }
public void send() throws MessagingException, IOException {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
helper.setFrom("user1@mail.com");
helper.setTo("user2@mail.com");
helper.setSubject("測試附件");
helper.setText("每日一笑,開開心心!!!");
// 附件 1
ClassPathResource file1 = new ClassPathResource("/attachfiles/test.doc");
helper.addAttachment(file1.getFilename(), file1.getFile());
// 附件 2(中文檔名需編碼)
ClassPathResource file2 = new ClassPathResource("/attachfiles/附件測試文件.doc");
helper.addAttachment(MimeUtility.encodeWord(file2.getFilename()), file2.getFile());
mailSender.send(msg);
}
}
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="192.168.17.176" />
<property name="port" value="25" />
<property name="username" value="user1" />
<property name="password" value="password1" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">5000</prop>
</props>
</property>
</bean>
<bean id="mailWithAttachment" class="com.example.MailWithAttachment">
<property name="mailSender" ref="mailSender" />
</bean>

mail.smtp.ssl.enable=true 或 STARTTLS,埠 465/587mail.smtp.ssl.trust=* 或導入信任庫mail.smtp.from、監聽彈回郵件MimeUtility.encodeWord