Java和Spring邮件的发送
方法一:
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("发送完毕...");
}
}
}
完毕....
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。