跪求JavaMail认证方式的设置
CRAM-MD5,LOGIN,PLAIN这几个如何在JavaMail属性中设置,哪为大哥知道的告诉小弟一下,谢谢了,在线等,下面是部分代码:
public boolean sendAlertMail(AlertConfBean alertConfBean)
{
String content = createAlertMail();
smtpHost = alertConfBean.getMailServer();
receiver = alertConfBean.getMailAddress();
userID = alertConfBean.getAuthUserID();
password = alertConfBean.getAuthPassword();
subject = alertConfBean.getMailHeader();
if (!checkMailField())
{
return false;
}
Properties properties = new Properties();
properties.put( "mail.smtp.host ", smtpHost);
properties.put( "mail.smtp.auth ", "true ");
Session session = Session.getDefaultInstance(properties,
new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userID, password);
}
});
MimeMessage mimeMsg = new MimeMessage(session);
try
{
mimeMsg.setFrom(new InternetAddress(sender));
mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver));
mimeMsg.setSubject(subject, "SHIFT-JIS ");
MimeBodyPart part = new MimeBodyPart();
part.setText(content == null ? " " : content, "SHIFT-JIS ");
part.setContent(content.toString(), "text/plain;charset=SHIFT-JIS ");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(part);
mimeMsg.setContent(multipart);
mimeMsg.setSentDate(new Date());
Transport.send(mimeMsg);
return true;
} catch (MessagingException exception)
{
m_log.error( "MessageException ", exception);
return false;
}
}
[解决办法]
public boolean connect() throws MessagingException, IOException {
if (isConnected())
return true;
// ユザはメル定がなければ、接をめる
if (!validation)
return false;
// ユザはメル定がなければ、接をめる
if (receiveHost == null)
return false;
if (receiveHost.length() == 0)
return false;
if (sendHost == null)
return false;
if (sendHost.length() == 0)
return false;
URLName url;
if (protocol.equalsIgnoreCase(PROTOCOL_IMAP4)) {
url = new URLName(protocol, receiveHost, -1, INBOX_FOLDERNAME, logonName,
logonPassword);
cat.debug( "Connect URL: " + protocol + ":// " + logonName + ":xxxxx@ "
+ receiveHost + "/ " + INBOX_FOLDERNAME);
} else {
String mboxdir = PropMngr.getTempDirPath();
// String mboxdir = PropMngr.getProperty( "MailDir ");
cat.debug( "MailBoxTempDir= " + mboxdir);
if (mboxdir == null) {
throw new MessagingException(
"POP3 Mail folder not found!Please check the properties file! ");
// mboxdir = "/mailbox/ ";
} else {
mboxdir += PropMngr.fileSeperator;
}
File dir = new File(mboxdir + empid);
if (!dir.exists()) {
cat.debug( "CreateDir: " + mboxdir + empid);
if (!dir.mkdirs()) {
throw new IOException( "Cannot create the directory: " + mboxdir
+ empid);
}
}
String storedir = "?localstore= " + mboxdir + empid;
url = new URLName(protocol, receiveHost, -1, storedir, logonName,
logonPassword);
cat.debug( "Connect URL: " + protocol + ":// " + logonName + ":xxxxx@ "
+ receiveHost + "/ " + storedir);
}
Properties props = System.getProperties();
props.put( "mail.smtp.host ", sendHost);
// ioffice.propertiesからパラメタ定をみ出し
String connTimeOutStr = PropMngr.getProperty( "ConnectionTimeOut ", "0 "); // seconds
String poolSizeStr = PropMngr.getProperty( "IMAPConnectionPoolSize ", "1 ");
int connTimeOut, poolSize;
try {
connTimeOut = 1000 * Integer.parseInt(connTimeOutStr); // milliseconds
poolSize = Integer.parseInt(poolSizeStr);
} catch (Exception e) {
connTimeOut = 0;
poolSize = 1;
}
if (protocol.equalsIgnoreCase(PROTOCOL_POP3)) {
props.put( "jp.gr.java_conf.roadster.net.pop.leaveOnServer ", new Boolean(
getDeleteServerMessage()).toString());
props.put( "jp.gr.java_conf.roadster.net.pop.checkInterval ", new Integer(
CHECK_INTERVAL).toString());
// POP3メルサバ接タイムアウトを定、TomcatThreadの度占用を回避するため
if (connTimeOut > 0) {
props.put( "jp.gr.java_conf.roadster.net.pop.timeout ", new Integer(
connTimeOut).toString());
}
} else {
// IMAP4メルサバ接タイムアウトを定、TomcatThreadの度占用を回避するため
if (connTimeOut > 0) {
props.put( "mail.imap.connectiontimeout ", new Integer(connTimeOut)
.toString());
props.put( "mail.imap.timeout ", new Integer(connTimeOut).toString());
}
if (poolSize > 1) {
props.put( "mail.imap.connectionpoolsize ", poolSizeStr);
}
}
try {
session = Session.getDefaultInstance(props, null);
// session = Session.getInstance(props, null );
session.setDebug(false);
store = session.getStore(url);
store.connect();
} catch (MessagingException me) {
connected = false;
throw me;
}
defaultFolder = store.getDefaultFolder();
if (defaultFolder == null)
throw new MessagingException( "No default folder ");
// fetchFolderList();
// PROTOCOL定
if (PROTOCOL_IMAP4.equals(this.protocol)) {
fSeparator = ". ";
} else if (PROTOCOL_POP3.equals(this.protocol)) {
// zhjun add 20051107
fSeparator = "/ ";
} else {
cat.debug( "PROTOCOLタイプ定例外。 " + this.protocol);
}
WebMailConstants.initFolderConst(fSeparator);
connected = true;
return true;
}
[解决办法]
修花的猫把日本人的代码都拿来了啊 好厉害!!呵呵
[解决办法]
也许是对日的呢,哈哈