当前位置:编程学习 > JSP >>

javamail发信的连接SMTP验证的问题

MailBean.java
package fastfox.util;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class MailBean {
public static void sendMessage(String smtpHost,String from,String to,String subject,String messageText)throws MessagingException{
System.out.println("Configuring mail session for:"+smtpHost);
SmtpAuth sa=new SmtpAuth();
sa.getuserinfo("jcren","mypassword");
java.util.Properties props=new java.util.Properties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtpHost);
System.out.println("Constructing message- from="+from+" to="+to);
InternetAddress fromAddress=new InternetAddress(from);
InternetAddress[] toAddresss=new InternetAddress[3];
toAddresss[0]=new InternetAddress("jcren@163.com");
toAddresss[1]=new InternetAddress("xxxxx@163.com");
toAddresss[2]=new InternetAddress("ccccc@163.com");
int i=0;
while(i<toAddresss.length){
Session mailSession=Session.getDefaultInstance(props,sa);
MimeMessage testMessage=new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddresss[i]);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(subject);
testMessage.setText(messageText);
System.out.println("Message constructed");
Transport.send(testMessage);
System.out.println("Message sent!");
i++;
}
}
public static void main(String[] args){
String smtpHost="smtp.163.com";
String from="jcren@163.com";
String to="jcren@163.com";
补充:Jsp教程,邮件
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,