問題描述
我正在使用自定義 SMTP 服務(wù)器,并希望在用戶輸入自己的服務(wù)器憑據(jù)時(shí)驗(yàn)證連接.
I'm using custom SMTP servers and would like to verify the connection when user enters his own server credentials.
與 Adob??e CF 和 Railo 在添加郵件服務(wù)器時(shí)允許執(zhí)行的檢查類型完全相同.
Exactly the same type of check as Adobe CF and Railo allow to do when adding mail server.
當(dāng)然,這并不能保證 delivery 會(huì)正常工作,但至少要檢查輸入的服務(wù)器/用戶名/密碼是否有效.
Sure, this does not guarantee that delivery will be working, but at least to check that entered server/username/pass are valid.
我可以看到一種棘手的方法:嘗試使用 cfmail 發(fā)送電子郵件并檢查郵件日志.但我相信它可以做得更優(yōu)雅.
I can see one tricky way: try to send the email with cfmail and check the mail log. But I believe that it can be done with more elegant.
標(biāo)準(zhǔn) ACF/Railo 發(fā)行版中是否有任何 Java 庫(kù)可以幫助我?我將如何使用它們?非常感謝示例.
Is there any Java library available with standard ACF/Railo distro to help me? How would I use them? Examples are highly appreciated.
提前致謝.
請(qǐng)不要與存在的 Java 標(biāo)記相混淆.CFML 中需要解決方案.雖然它可以使用一些 Java 庫(kù)(如果適用).
Please don't be confused with Java tag present. Solution needed in CFML. Though it can use some Java libraries, if applicable.
推薦答案
我認(rèn)為 sfussenegger 的想法是對(duì)的.但是不是使用自定義身份驗(yàn)證器,而是通過 connect(..) 進(jìn)行身份驗(yàn)證呢?只用gmail測(cè)試過.但它似乎工作.
I think sfussenegger has the right idea. But instead of using a custom authenticator, what about authenticating via connect(..)? Only tested with gmail. But it seems to work.
我用 CF9 &OBD成功.不幸的是,我在 Railo 上沒有運(yùn)氣……真可惜.
I tested this with CF9 & OBD successfully. Unfortunately, I had no luck with Railo ... bummer.
已更新以添加缺少的mail.smtp.auth"屬性.它現(xiàn)在應(yīng)該也可以與 Railo 一起正常工作了.
Updated to add the missing "mail.smtp.auth" property. It should now work correctly with Railo as well.
//Java Version
int port = 587;
String host = "smtp.gmail.com";
String user = "username@gmail.com";
String pwd = "email password";
try {
Properties props = new Properties();
// required for gmail
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
// or use getDefaultInstance instance if desired...
Session session = Session.getInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect(host, port, user, pwd);
transport.close();
System.out.println("success");
}
catch(AuthenticationFailedException e) {
System.out.println("AuthenticationFailedException - for authentication failures");
e.printStackTrace();
}
catch(MessagingException e) {
System.out.println("for other failures");
e.printStackTrace();
}
<cfscript>
//CF Version
port = 587;
host = "smtp.gmail.com";
user = "username@gmail.com";
pwd = "email password";
try {
props = createObject("java", "java.util.Properties").init();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// or use getDefaultInstance instance if desired...
mailSession = createObject("java", "javax.mail.Session").getInstance(props, javacast("null", ""));
transport = mailSession.getTransport("smtp");
transport.connect(host, port, user, pwd);
transport.close();
WriteOutput("success");
}
//for authentication failures
catch(javax.mail.AuthenticationFailedException e) {
WriteOutput("Error: "& e.type &" ** "& e.message);
}
// for other failures
catch(javax.mail.MessagingException e) {
WriteOutput("Error: "& e.type &" ** "& e.message);
}
</cfscript>
這篇關(guān)于在 ColdFusion 中以編程方式驗(yàn)證郵件服務(wù)器連接的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!