实现 一个邮件发送功能
2014-05-05 10:39
351 查看
规则 SendMail 001 :
Sub Initialize
On Error Goto errormsg
Dim ss As New NotesSession
Dim db As NotesDatabase
Dim infdb As NotesDatabase
Dim agent As NotesAgent
dim tempdoc as NotesDocument
dim sendto1(20) as string '定义了一个20人的数组存放 收件人
Dim sendfrom As string
dim sendto as string
Dim smtpserver As string
Dim username As string
Dim pwd As string
Dim title As string
Dim s_subject As string
msgbox WF_Document.sendto(0)
sendto1(0)=WF_Document.sendto(0)
'msgbox "sendto1(0)==============="+sendto1(0)
Set db = ss.CurrentDatabase
set infdb=ss.getdatabase(db.server, "bpm/interface.nsf",false)
set tempdoc=infdb.CreateDocument
tempdoc.Form="TempForm1"
set agent=infdb.GetAgent("SendMailAgent")
set ctxdoc=ss.DocumentContext
host=ctxdoc.SERVER_NAME(0)
host="http://"+host
'设置邮件标题、内容等
tempdoc.Subject=WF_Document.title(0)
tempdoc.Sendto=sendto1
tempdoc.content="<html><body>"+WF_Document.s_subject(0)+"</body></html>"
tempdoc.smtpserver=WF_Document.smtpserver(0)
tempdoc.sendfrom=WF_Document.sendfrom(0)
tempdoc.username=WF_Document.username(0)
tempdoc.pwd=WF_Document.pwd(0)
call tempdoc.save(1,0)
agent.run(tempdoc.Noteid)
'msgbox "发送成功"
Exit Sub
errormsg:
Msgbox "Rule Error:" & Str(Erl) & " " & Error
End Sub
代理 SendEmailAgent:
Use "javamail2"
Use "linkeyworkflow_sharefunction"
Sub Initialize
Dim ss As New NotesSession
Dim agent As NotesAgent
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim revs As Variant
On Error Resume Next
Dim myClass As JavaClass
Dim myObject As JavaObject
Dim mySession As New JavaSession
Set myClass = mySession.getClass("Mail")
Set myObject = myClass.CreateObject()
Set db=ss.Currentdatabase
Set agent=ss.Currentagent
Set doc=db.Getdocumentbyid(agent.Parameterdocid)
revs=doc.Getitemvalue("Sendto")
If Not doc Is Nothing Then
Call myObject.sendMailNoFile(revs,doc.Subject(0),doc.content(0),doc.smtpserver(0),doc.sendfrom(0),doc.username(0),doc.pwd(0))
doc.Remove(1)
End If
End Sub
java类:
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.internet.MimeUtility;
import java.io.*;
import java.io.File;
public class Mail {
private String host = "smtp.163.com";
private String mail_head_name = "this is head of this mail";
private String mail_head_value = "this is head of this mail";
private String mail_from = "15889985184@163.com";
private String personalName = "Alex测试";//相当于称呼,通常显示在你的发件人栏的发件人邮箱地址前
private String fileUrl;
private String fileName;
private String username;
private String password;
public void sendMailNoFile(String[] mail_to,String mail_subject,String mail_body,String mail_smtpserver,String mail_sendfrom,String mail_username,String mail_pwd) throws SendFailedException {
//public void sendMail(String mail_to,String mail_subject,String mail_body) throws SendFailedException {
try {
Properties props = new Properties();// 获取系统环境
//System.out.println("sssss="+mail_smtpserver);
props.put("mail.smtp.host", mail_smtpserver);
System.out.println("111111111111111111111");
props.put("mail.smtp.auth", "true");
username=mail_username;
password=mail_pwd;
Authenticator auth = new Email_Autherticator(username,password);// 进行邮件服务用户认证
Session session = Session.getInstance(props, auth);// 设置session,和邮件服务器进行通讯
for (int i=0;i<mail_to.length;i++) {
if (!mail_to[i].trim().equals("")){
InternetAddress inetAddr=new InternetAddress(mail_to[i]);
MimeMessage message = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mail_
4000
body, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
message.saveChanges();
message.setSubject(mail_subject);// 设置邮件主题
message.setHeader(mail_head_name, mail_head_value);// 设置邮件标题
message.setSentDate(new Date());// 设置邮件发送时期
Address address = new InternetAddress(mail_from, mail_sendfrom);
message.setFrom(address);// 设置邮件发送者的地址
message.addRecipient(Message.RecipientType.TO, inetAddr);
Transport.send(message);
}
}
/*
ArrayList addrList=new ArrayList();
for (int i=0;i<mail_to.length;i++) {
if (!mail_to[i].trim().equals("")){
addrList.add(new InternetAddress(mail_to[i]));
}
}
InternetAddress[] inetAddr=(InternetAddress[])addrList.toArray(new InternetAddress[addrList.size()]);
*/
//Address toaddress = new InternetAddress(mail_to);// 设置邮件接收者的地址
//Address toaddress = new InternetAddress("sjl_com@163.com");// 设置邮件接收者的地址
//System.out.println("3");
//message.addRecipient(Message.RecipientType.TO, toaddress);
//message.addRecipients(Message.RecipientType.TO, inetAddr);
//System.out.println("4");
//System.out.println("message="+message);
//抱错行
//Transport.send(message);
System.out.print("mail sent...");
//System.out.println("5");
} catch (Exception e) {
//System.out.println("Java_Message:");
System.out.println("SMTP邮件发送失败!");
e.printStackTrace();
}
// return flag;
}
public void sendMail(String mail_to,String mail_subject,String mail_body,String fileList) throws SendFailedException {
//public void sendMail(String mail_to,String mail_subject,String mail_body) throws SendFailedException {
try {
Properties props = new Properties();// 获取系统环境
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
//System.out.println("props="+props);
Authenticator auth = new Email_Autherticator();// 进行邮件服�裼没现�
Session session = Session.getInstance(props, auth);// 设置session,和邮件服务器进行通讯
MimeMessage message = new MimeMessage(session);
//message.setContent(mail_body, "text/html; charset=utf-8");// 设置邮件格式
/**
* setText("Email Content. ")默认的MIME类型是 text/plain如果只是发普通文本,
* 可以用它来代替setContent("Hello", "text/plain")
*/
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
if (fileList != ""){
System.out.println("fileList="+fileList);
String[] array = fileList.split (",");
if (array.length > 0) {
for (int i = 0; i < array.length; i ++) {
fileUrl = array[i];
//fileName = array[i].split (";")[1];
System.out.println(fileUrl);
messageBodyPart = new MimeBodyPart();
File localFile = new File(fileUrl);
DataSource source = new FileDataSource(localFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(MimeUtility.encodeText(localFile.getName()));
multipart.addBodyPart(messageBodyPart);
}
}
}
messageBodyPart = new MimeBodyPart();
//messageBodyPart.setText("Hi");//邮件正文内容
messageBodyPart.setContent(mail_body, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
message.saveChanges();
message.setSubject(mail_subject);// 设置邮件主题
//message.setText(mail_body);// 设置邮件内容
message.setHeader(mail_head_name, mail_head_value);// 设置邮件标题
message.setSentDate(new Date());// 设置邮件发送时期
Address address = new InternetAddress(mail_from, personalName);
message.setFrom(address);// 设置邮件发送者的地址
Address toaddress = new InternetAddress(mail_to);// 设置邮件接收者的地址
//Address toaddress = new InternetAddress("sjl_com@163.com");// 设置邮件接收者的地址
message.addRecipient(Message.RecipientType.TO, toaddress);
//抱错行
Transport.send(message);
} catch (Exception e) {
System.out.println("邮件发送发送失败!");
e.printStackTrace();
}
// return flag;
}
}
Email_Autherticator 类:
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator {
// String username = "15889985184@163.com";
// String password = "lishuyuan7224";
String username;
String password;
public Email_Autherticator() {
super();
}
public Email_Autherticator(String user,String pwd){
super();
username = user;
password = pwd;
}
//Authenticator是抽象类,必须实现它的getPasswordAuthentication
//方法返回一个PasswordAuthentication对象
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}
Sub Initialize
On Error Goto errormsg
Dim ss As New NotesSession
Dim db As NotesDatabase
Dim infdb As NotesDatabase
Dim agent As NotesAgent
dim tempdoc as NotesDocument
dim sendto1(20) as string '定义了一个20人的数组存放 收件人
Dim sendfrom As string
dim sendto as string
Dim smtpserver As string
Dim username As string
Dim pwd As string
Dim title As string
Dim s_subject As string
msgbox WF_Document.sendto(0)
sendto1(0)=WF_Document.sendto(0)
'msgbox "sendto1(0)==============="+sendto1(0)
Set db = ss.CurrentDatabase
set infdb=ss.getdatabase(db.server, "bpm/interface.nsf",false)
set tempdoc=infdb.CreateDocument
tempdoc.Form="TempForm1"
set agent=infdb.GetAgent("SendMailAgent")
set ctxdoc=ss.DocumentContext
host=ctxdoc.SERVER_NAME(0)
host="http://"+host
'设置邮件标题、内容等
tempdoc.Subject=WF_Document.title(0)
tempdoc.Sendto=sendto1
tempdoc.content="<html><body>"+WF_Document.s_subject(0)+"</body></html>"
tempdoc.smtpserver=WF_Document.smtpserver(0)
tempdoc.sendfrom=WF_Document.sendfrom(0)
tempdoc.username=WF_Document.username(0)
tempdoc.pwd=WF_Document.pwd(0)
call tempdoc.save(1,0)
agent.run(tempdoc.Noteid)
'msgbox "发送成功"
Exit Sub
errormsg:
Msgbox "Rule Error:" & Str(Erl) & " " & Error
End Sub
代理 SendEmailAgent:
Use "javamail2"
Use "linkeyworkflow_sharefunction"
Sub Initialize
Dim ss As New NotesSession
Dim agent As NotesAgent
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim revs As Variant
On Error Resume Next
Dim myClass As JavaClass
Dim myObject As JavaObject
Dim mySession As New JavaSession
Set myClass = mySession.getClass("Mail")
Set myObject = myClass.CreateObject()
Set db=ss.Currentdatabase
Set agent=ss.Currentagent
Set doc=db.Getdocumentbyid(agent.Parameterdocid)
revs=doc.Getitemvalue("Sendto")
If Not doc Is Nothing Then
Call myObject.sendMailNoFile(revs,doc.Subject(0),doc.content(0),doc.smtpserver(0),doc.sendfrom(0),doc.username(0),doc.pwd(0))
doc.Remove(1)
End If
End Sub
java类:
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.internet.MimeUtility;
import java.io.*;
import java.io.File;
public class Mail {
private String host = "smtp.163.com";
private String mail_head_name = "this is head of this mail";
private String mail_head_value = "this is head of this mail";
private String mail_from = "15889985184@163.com";
private String personalName = "Alex测试";//相当于称呼,通常显示在你的发件人栏的发件人邮箱地址前
private String fileUrl;
private String fileName;
private String username;
private String password;
public void sendMailNoFile(String[] mail_to,String mail_subject,String mail_body,String mail_smtpserver,String mail_sendfrom,String mail_username,String mail_pwd) throws SendFailedException {
//public void sendMail(String mail_to,String mail_subject,String mail_body) throws SendFailedException {
try {
Properties props = new Properties();// 获取系统环境
//System.out.println("sssss="+mail_smtpserver);
props.put("mail.smtp.host", mail_smtpserver);
System.out.println("111111111111111111111");
props.put("mail.smtp.auth", "true");
username=mail_username;
password=mail_pwd;
Authenticator auth = new Email_Autherticator(username,password);// 进行邮件服务用户认证
Session session = Session.getInstance(props, auth);// 设置session,和邮件服务器进行通讯
for (int i=0;i<mail_to.length;i++) {
if (!mail_to[i].trim().equals("")){
InternetAddress inetAddr=new InternetAddress(mail_to[i]);
MimeMessage message = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mail_
4000
body, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
message.saveChanges();
message.setSubject(mail_subject);// 设置邮件主题
message.setHeader(mail_head_name, mail_head_value);// 设置邮件标题
message.setSentDate(new Date());// 设置邮件发送时期
Address address = new InternetAddress(mail_from, mail_sendfrom);
message.setFrom(address);// 设置邮件发送者的地址
message.addRecipient(Message.RecipientType.TO, inetAddr);
Transport.send(message);
}
}
/*
ArrayList addrList=new ArrayList();
for (int i=0;i<mail_to.length;i++) {
if (!mail_to[i].trim().equals("")){
addrList.add(new InternetAddress(mail_to[i]));
}
}
InternetAddress[] inetAddr=(InternetAddress[])addrList.toArray(new InternetAddress[addrList.size()]);
*/
//Address toaddress = new InternetAddress(mail_to);// 设置邮件接收者的地址
//Address toaddress = new InternetAddress("sjl_com@163.com");// 设置邮件接收者的地址
//System.out.println("3");
//message.addRecipient(Message.RecipientType.TO, toaddress);
//message.addRecipients(Message.RecipientType.TO, inetAddr);
//System.out.println("4");
//System.out.println("message="+message);
//抱错行
//Transport.send(message);
System.out.print("mail sent...");
//System.out.println("5");
} catch (Exception e) {
//System.out.println("Java_Message:");
System.out.println("SMTP邮件发送失败!");
e.printStackTrace();
}
// return flag;
}
public void sendMail(String mail_to,String mail_subject,String mail_body,String fileList) throws SendFailedException {
//public void sendMail(String mail_to,String mail_subject,String mail_body) throws SendFailedException {
try {
Properties props = new Properties();// 获取系统环境
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
//System.out.println("props="+props);
Authenticator auth = new Email_Autherticator();// 进行邮件服�裼没现�
Session session = Session.getInstance(props, auth);// 设置session,和邮件服务器进行通讯
MimeMessage message = new MimeMessage(session);
//message.setContent(mail_body, "text/html; charset=utf-8");// 设置邮件格式
/**
* setText("Email Content. ")默认的MIME类型是 text/plain如果只是发普通文本,
* 可以用它来代替setContent("Hello", "text/plain")
*/
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
if (fileList != ""){
System.out.println("fileList="+fileList);
String[] array = fileList.split (",");
if (array.length > 0) {
for (int i = 0; i < array.length; i ++) {
fileUrl = array[i];
//fileName = array[i].split (";")[1];
System.out.println(fileUrl);
messageBodyPart = new MimeBodyPart();
File localFile = new File(fileUrl);
DataSource source = new FileDataSource(localFile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(MimeUtility.encodeText(localFile.getName()));
multipart.addBodyPart(messageBodyPart);
}
}
}
messageBodyPart = new MimeBodyPart();
//messageBodyPart.setText("Hi");//邮件正文内容
messageBodyPart.setContent(mail_body, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
message.saveChanges();
message.setSubject(mail_subject);// 设置邮件主题
//message.setText(mail_body);// 设置邮件内容
message.setHeader(mail_head_name, mail_head_value);// 设置邮件标题
message.setSentDate(new Date());// 设置邮件发送时期
Address address = new InternetAddress(mail_from, personalName);
message.setFrom(address);// 设置邮件发送者的地址
Address toaddress = new InternetAddress(mail_to);// 设置邮件接收者的地址
//Address toaddress = new InternetAddress("sjl_com@163.com");// 设置邮件接收者的地址
message.addRecipient(Message.RecipientType.TO, toaddress);
//抱错行
Transport.send(message);
} catch (Exception e) {
System.out.println("邮件发送发送失败!");
e.printStackTrace();
}
// return flag;
}
}
Email_Autherticator 类:
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator {
// String username = "15889985184@163.com";
// String password = "lishuyuan7224";
String username;
String password;
public Email_Autherticator() {
super();
}
public Email_Autherticator(String user,String pwd){
super();
username = user;
password = pwd;
}
//Authenticator是抽象类,必须实现它的getPasswordAuthentication
//方法返回一个PasswordAuthentication对象
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}
相关文章推荐
- 另外一个ASP.NET 实现邮件发送和接受的功能(Sockets)
- 另外一个ASP.NET 实现邮件发送和接受的功能(Sockets)
- Kettle实现邮件发送并带附件功能
- python3.x实现发送邮件功能
- 在java程序中实现发送邮件的功能
- thinkphp实现发送邮件密码找回功能实例
- SAP NetWeaver CE中实现邮件的发送功能
- Python smtplib实现发送邮件功能
- 实现OWA 2010通过短信方式发送邮件提醒功能
- 使用javamail实现邮件发送功能完整代码
- 在程序中实现邮件发送,电话拨打,sms,以及浏览器功能能的方法整理
- 一个发送邮件功能,用户反映没有收到邮件,如何处理bug
- 实现javaEmail邮件发送功能并带有生成的pdf附件
- 利用SAP Offcie组件实现邮件发送功能
- python实现发送邮件及附件功能
- SQL Server 2008 通过配置数据库邮件实现发送邮件功能
- thinkphp 3.2.3 在项目中需要做的一个发送邮件的功能
- C#实现邮件发送的功能
- 在java程序中实现发送邮件的功能
- 通过log4j实现发送错误信息的邮件功能