//要把用户的注册邮箱存在数据库,然后用javamail发送过去。
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class javaMail
...{
private boolean bAuth =false; //SMTP服务器是否需要认证
private Vector vfile = new Vector(10,10); //附件列表变量
private Vector vmsg = new Vector(10,10); //信息体变量
public String from="pdms@amoi.com.cn"; //邮件发送者
private String to=""; //邮件接收者
public static String smtpHost="10.100.51.12"; //邮件服务器
private String context=""; //邮件正文
private String subject=""; //邮件主题
private String uid=""; //邮件帐号
private String pwd=""; //邮件密码
private String filename=""; //附件路径
public class SmtpAuth extends Authenticator //一个发信认证类
...{
public String User;
public String Pass;
public SmtpAuth(String user,String pass)
...{
this.User = user;
this.Pass = pass;
}
protected PasswordAuthentication getPasswordAuthentication()
...{
return new PasswordAuthentication(User,Pass);
}
}
public boolean isValidEmail(String s)//利用正则表达表判断是否是用效的Email地址
...{
if(s==null) s="";
String regx = "/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*";
if(s.matches(regx))
return true;
else
return false;
}
public void AddAttach(String s) //收集附加文件
...{
if(s!=null)
vfile.addElement(s);
}
private void setMessage(String s) //收集邮件正文
...{
if(s!=null)
vmsg.addElement(s);
}
//发送带认证邮件
public boolean sendMail(String To,String Subject,String Content,String Uid,String Pwd)
...{
to=To;
subject=Subject;
setMessage(Content);
uid = Uid;
pwd = Pwd;
bAuth = true;
return startSend();
}
//发送不带认证邮件
public boolean sendMail(String To,String Subject,String Content)
...{
to=To;
subject=Subject;
setMessage(Content);
bAuth = false;
return startSend();
}
public javaMail()...{}
//发送邮件过程
private boolean startSend()
...{
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
Session session;
if(bAuth)
...{
SmtpAuth sa = new SmtpAuth(uid,pwd);
props.put("mail.smtp.auth","true");
session=Session.getInstance(props, sa);
}
else
...{
session=Session.getDefaultInstance(props, null);
}
try
...{
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
StringTokenizer st=new StringTokenizer(to,";");
InternetAddress toAddress[] = new InternetAddress[st.countTokens()];
int i=0;
while(st.hasMoreTokens())
...{
toAddress[i]= new InternetAddress(st.nextToken());
i++;
}
msg.setRecipients(Message.RecipientType.TO,toAddress);
msg.setSubject(subject);
//把message part加入新创建的Multipart
Multipart mp = new MimeMultipart();
// 邮件内容的第一部分
Enumeration emsg=vmsg.elements();
while(emsg.hasMoreElements())
...{
context=emsg.nextElement().toString();
MimeBodyPart mbp1=new MimeBodyPart();
mbp1.setText(context);
mp.addBodyPart(mbp1);
}
vmsg.removeAllElements();
// 邮件内容的第二部分
Enumeration efile=vfile.elements();
while(efile.hasMoreElements())
...{
MimeBodyPart mbp2=new MimeBodyPart();
filename=efile.nextElement().toString();
FileDataSource fds=new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mp.addBodyPart(mbp2);
}
vfile.removeAllElements(); // 把MultiPart加入邮件
msg.setContent(mp); // 设置邮件头的发送日期
msg.setSentDate(new Date());
// 发送邮件
Transport.send(msg);
}
catch (MessagingException mex)
...{
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null)
...{
ex.printStackTrace();
}
return false;
}
catch(Exception e)
...{
System.out.println("Catched Exception:"+e.toString());
}
return true;
}
public static String getSmtpHost()
...{
return smtpHost.trim();
}
public static void setSmtpHost(String mailHost)
...{
smtpHost=mailHost.trim();
}
相关文章
- php 使用phpmailer 发送邮件(附带中文乱码的解决方法)
- 为 Confluence 6 配置发送邮件消息
- 发送邮件(遵循smtp协议即简单的邮件发送协议)
- 黑马程序员--Java基础学习之网络编程(TCP、UDP、Socket、模拟发送和接收数据)
- 黑马程序员--Java基础学习之网络编程(TCP、UDP、Socket、模拟发送和接收数据)
- 手把手教你接入抖音小程序发送模板消息通知
- C#基础--基于POP3协议的邮件接收和基于STMP的邮件发送
- JSP学习1---创建一个简单的jsp程序
- Flask-email 发送邮件的配置,发送附件的方法,以及os.environ.get('MAIL_USERNAME')为None的解决办法
- 黑马程序员——jsp的最常用的5大内置对象