简单的发送mail


import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
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 Javamail {
// Member Variables
public String from;
public String to;
public String bcc;
public String cc;
public String replyto;
public String host;
public String subject;
public String body;
public String attachments[];

public EmailBean() {
}

public EmailBean(String host, String from, String to, String bcc,
String cc, String replyto, String subject, String body,
String attachments[]) {
this.host = host;
this.from = from;
this.to = to;
this.bcc = bcc;
this.cc = cc;
this.replyto = replyto;
this.subject = subject;
this.body = body;
this.attachments = attachments;
}

public boolean sendMail() throws Exception {
boolean send = true;
try {
logObj.info("EmailBean.sendMail()");
logObj.info("EmailBean.sendMail() :: Host " + host);
logObj.info("EmailBean.sendMail() :: Body " + body);
logObj.info("EmailBean.sendMail() :: To " + to);
if (this.to == null)
return false;
if (this.host == null)
return false;
if (this.body == null)
return false;

InternetAddress[] IA_address = InternetAddress.parse(this.to);
InternetAddress[] IA_bcc = null;
InternetAddress[] IA_cc = null;
InternetAddress[] IA_replyto = null;

if (!this.bcc.equals("")) {
IA_bcc = InternetAddress.parse(this.bcc);
}
if (!this.cc.equals("")) {
IA_cc = InternetAddress.parse(this.cc);
}
if (!this.replyto.equals("")) {
IA_replyto = InternetAddress.parse(this.replyto);
}
Properties props = System.getProperties();
props.put("mail.smtp.host", this.host);
// Get session
Session session = Session.getInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(this.from));
message.setRecipients(Message.RecipientType.TO, IA_address);
if (IA_bcc != null) {
message.addRecipients(Message.RecipientType.BCC, IA_bcc);
}
if (IA_cc != null) {
message.addRecipients(Message.RecipientType.CC, IA_cc);
}
if (IA_replyto != null) {
// message.setReplyTo(new InternetAddress(this.replyto));
message.setReplyTo(IA_replyto);
}
message.setSubject(this.subject);

// Multipart
Multipart multipart = new MimeMultipart();

// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// fill message
messageBodyPart.setText(this.body);

// Add message part to multipart
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
if (this.attachments != null) {
for (int i = 0; (i < this.attachments.length); i++) {
try {
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(
this.attachments[i]);
messageBodyPart.setDataHandler(new DataHandler(source));

int fnm_index = this.attachments[i].lastIndexOf("/");
if (fnm_index < 0) {
fnm_index = this.attachments[i].lastIndexOf("\\");
}
if (fnm_index < 0) {
fnm_index = 0;
}
messageBodyPart.setFileName(this.attachments[i]
.substring(fnm_index));

multipart.addBodyPart(messageBodyPart);
} catch (Exception e) {
logObj
.error("EmailBean.sendMail() :: error while attaching files "
+ e);
throw e;
}
}
}

// Put parts in message
message.setContent(multipart);

Transport.send(message);

} catch (Exception e) {
logObj.error("EmailBean.sendMail() :: Exception  " + e);
send = false;
throw e;
}
logObj.info("EmailBean.sendMail() :: An email to " + this.to
+ " has been generated");
return send;
}

public static void main(String[] args) {
String message = "HEY MAN, WHAT A NICE DAY!";
String[] attachments = {"G:\\workspace\\eBill\\src\\com\\bax\\util\\Application.java","G:\\workspace\\eBill\\src\\com\\bax\\util\\BAXLogger.java"};
EmailBean mail = new EmailBean("10.215.22.88",
"[email protected]",
"[email protected]", "", "", "",
"DEMO FOR LOCAL MAIL SENDING", message, attachments);
try {
boolean send = mail.sendMail();
System.out.println("mail sent:" + send);
} catch (Exception e) {
e.printStackTrace();
}

}
}

你可能感兴趣的:(mail)