標籤:
方法一:
java寄送電子郵件:這裡以發送qq郵件為例:
package test;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
public class MySendEmail {
public static void main(String[] args) {
try {
String userName = "發送方qq號@qq.com";
String password = "發送方郵箱密碼";
String smtp_server = "smtp.qq.com"; //qq郵箱郵件伺服器地址
String from_mail_address = userName;
String to_mail_address = "接收方qq號@qq.com";
Authenticator auth = new PopupAuthenticator(userName, password);
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", smtp_server);
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", userName);
mailProps.put("password", password);
Session mailSession = Session.getDefaultInstance(mailProps, auth);
mailSession.setDebug(true);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from_mail_address));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
to_mail_address));
message.setSubject("Mail Testw");
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("Java電子郵件測試內容w");
// textBodyPart.setFileName("37af4739a11fc9d6b311c712.jpg");
textBodyPart.setFileName("E:\\API\\W3C.chm");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
Transport.send(message);
} catch (Exception ex) {
System.err.println("郵件發送失敗的原因是:" + ex.getMessage());
System.err.println("具體的錯誤原因");
ex.printStackTrace(System.err);
}
}
}
class PopupAuthenticator extends Authenticator {
private String username;
private String password;
public PopupAuthenticator(String username, String pwd) {
this.username = username;
this.password = pwd;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
——————————————————————————————————————————————————————------————————————————————————
方法二: 用Spring提供的發郵件功能,同樣以發送qq郵箱為例:
beans.xml檔案代碼:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 注意:這裡的參數(如使用者名稱、密碼)都是針對郵件寄件者的 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>smtp.qq.com</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="username">
<value>發送方qq號@qq.com</value>
</property>
<property name="password">
<value>發送方qq郵箱密碼</value>
</property>
</bean>
</beans>
發送郵件的Java代碼:
package test;
import java.util.UUID;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class SendMail {
public ApplicationContext ctx = null;
public SendMail() {
//擷取上下文
ctx = new ClassPathXmlApplicationContext("beans.xml");
}
public void send() {
//擷取JavaMailSender bean
JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
SimpleMailMessage mail = new SimpleMailMessage(); //<SPAN style="COLOR: #ff0000">注意SimpleMailMessage只能用來發送text格式的郵件</SPAN>
try {
mail.setTo("接收者@qq.com");//接受者
mail.setFrom("寄件者@qq.com");//寄件者,這裡還可以另起Email別名,不用和xml裡的username一致
mail.setSubject("spring mail test!");//主題
mail.setText("springMail的簡單發送測試,附帶隨機驗證碼為:"+UUID.randomUUID().toString().substring(0, 6));//郵件內容
sender.send(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
SendMail sendMail = new SendMail();
sendMail.send();
System.out.println("發送完畢...");
}
}
}
完畢....
Java和Spring郵件的發送