jmail 4.5使用gmail smtp ssl解决方法

现在很多email都没法提供smtp发信,但是gmail这个稳定的平台给了很多网站站长的一个希望。可惜就是smtp.gmail.com 是用ssl发送,一般的情况下asp+jmail是没法发送的,但是找了半天才找到以下的代码可以解决以上的问题。

jmail 使用gmail smtp.gmail.com 发送

package org.dreams.mail;

import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.util.date;
import java.util.properties;

import javax.mail.authenticator;
import javax.mail.folder;
import javax.mail.message;
import javax.mail.messagingexception;
import javax.mail.passwordauthentication;
import javax.mail.sendfailedexception;
import javax.mail.session;
import javax.mail.store;
import javax.mail.urlname;
import javax.mail.internet.internetaddress;
import javax.mail.internet.mimebodypart;
import javax.mail.internet.mimemessage;
import javax.mail.internet.mimemultipart;

import com.sun.mail.smtp.smtpaddressfailedexception;
import com.sun.mail.smtp.smtpaddresssucceededexception;
import com.sun.mail.smtp.smtpsendfailedexception;
import com.sun.mail.smtp.smtptransport;

public class jmailsslsender {

public static void main(string[] argv) {
system.setproperty("javax.net.ssl.truststore", "c:\\javalib\\ssl\\mail");//密钥存放的位置
system.setproperty("javax.net.ssl.truststorepassword", "打开密钥文件的密码");

string to = "收信者地址";
string subject = "hello";//邮件标题
string from = "送信者地址";
string cc = "抄送者地址";
string bcc = null;
string url = null;

string mailhost = "smtp.gmail.com:465";//邮件服务器地址
string mailer = "jmailsslsender";
string file = null;
string protocol = null;
string host = "smtp.gmail.com:465";
string user = "用户名";
string password = "密码";

string record = null; // name of folder in which to record mail
boolean debug = true;
boolean verbose = true;
boolean auth = true;
boolean ssl = true;
bufferedreader in = new bufferedreader(new inputstreamreader(system.in));
int optind;

try {

properties props = system.getproperties();
if (mailhost != null)
props.put("mail.smtp.host", mailhost);

if (auth) {
if (ssl) {
props.put("mail.smtps.auth", "true");//ssl连接使用的属性
} else {
props.put("mail.smtp.auth", "true");//普通连接的属性
}

}

// get a session object
session session = session.getinstance(props, new authenticator() {
protected passwordauthentication getpasswordauthentication() {
return new passwordauthentication("用户名", "密码");
}
});

if (debug) {
session.setdebug(true);
}

// construct the message
message msg = new mimemessage(session);
if (from != null)
msg.setfrom(new internetaddress(from));
else
msg.setfrom();

msg.setrecipients(message.recipienttype.to, internetaddress.parse(to, false));
if (cc != null)
msg.setrecipients(message.recipienttype.cc, internetaddress.parse(cc, false));
if (bcc != null)
msg.setrecipients(message.recipienttype.bcc, internetaddress.parse(bcc, false));

msg.setsubject(subject);

string text = "i am liu!";//collect(in);

if (file != null) {
// attach the specified file.
// we need a multipart message to hold the attachment.
mimebodypart mbp1 = new mimebodypart();
mbp1.settext(text);
mimebodypart mbp2 = new mimebodypart();
mbp2.attachfile(file);
mimemultipart mp = new mimemultipart();
mp.addbodypart(mbp1);
mp.addbodypart(mbp2);
msg.setcontent(mp);
} else {
// if the desired charset is known, you can use
// settext(text, charset)
msg.settext(text);
}

msg.setheader("x-mailer", mailer);
msg.setsentdate(new date());

// send the thing off
/*
* the simple way to send a message is this:
*
transport.send(msg);
*
* but we're going to use some smtp-specific features for
* demonstration purposes so we need to manage the transport
* object explicitly.
*/
smtptransport t = (smtptransport) session.gettransport(ssl ? "smtps" : "smtp");
try {
if (auth)
t.connect(mailhost, user, password);
else
t.connect();

t.sendmessage(msg, msg.getallrecipients());
} finally {
if (verbose)
system.out.println("response: " + t.getlastserverresponse());
t.close();
}

system.out.println("\nmail was sent successfully.");

// keep a copy, if requested.

if (record != null) {
// get a store object
store store = null;
if (url != null) {
urlname urln = new urlname(url);
store = session.getstore(urln);
store.connect();
} else {
if (protocol != null)
store = session.getstore(protocol);
else
store = session.getstore();

// connect
if (host != null || user != null || password != null)
store.connect(host, user, password);
else
store.connect();
}

// get record folder. create if it does not exist.
folder folder = store.getfolder(record);
if (folder == null) {
system.err.println("can't get record folder.");
system.exit(1);
}
if (!folder.exists())
folder.create(folder.holds_messages);

message[] msgs = new message[1];
msgs[0] = msg;
folder.appendmessages(msgs);

system.out.println("mail was recorded successfully.");
}

} catch (exception e) {
if (e instanceof sendfailedexception) {
messagingexception sfe = (messagingexception) e;
if (sfe instanceof smtpsendfailedexception) {
smtpsendfailedexception ssfe = (smtpsendfailedexception) sfe;
system.out.println("smtp send failed:");
if (verbose)
system.out.println(ssfe.tostring());
system.out.println(" command: " + ssfe.getcommand());
system.out.println(" retcode: " + ssfe.getreturncode());
system.out.println(" response: " + ssfe.getmessage());
} else {
if (verbose)
system.out.println("send failed: " + sfe.tostring());
}
exception ne;
while ((ne = sfe.getnextexception()) != null && ne instanceof messagingexception) {
sfe = (messagingexception) ne;
if (sfe instanceof smtpaddressfailedexception) {
smtpaddressfailedexception ssfe = (smtpaddressfailedexception) sfe;
system.out.println("address failed:");
if (verbose)
system.out.println(ssfe.tostring());
system.out.println(" address: " + ssfe.getaddress());
system.out.println(" command: " + ssfe.getcommand());
system.out.println(" retcode: " + ssfe.getreturncode());
system.out.println(" response: " + ssfe.getmessage());
} else if (sfe instanceof smtpaddresssucceededexception) {
system.out.println("address succeeded:");
smtpaddresssucceededexception ssfe = (smtpaddresssucceededexception) sfe;
if (verbose)
system.out.println(ssfe.tostring());
system.out.println(" address: " + ssfe.getaddress());
system.out.println(" command: " + ssfe.getcommand());
system.out.println(" retcode: " + ssfe.getreturncode());
system.out.println(" response: " + ssfe.getmessage());
}
}
} else {
system.out.println("got exception: " + e);
if (verbose)
e.printstacktrace();
}
}
}

public static string collect(bufferedreader in) throws ioexception {
string line;
stringbuffer sb = new stringbuffer();
while ((line = in.readline()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.tostring();
}
}

你可能感兴趣的:(.net,asp.net,asp,sun,Gmail)