[英]How to Send Emails in GWT Application
我正在使用此代碼在我的Java應用程序中發送電子郵件
try{
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("me@gmail.com", "mypwd"));
email.setTLS(true);
email.setFrom("me@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("me@hotmail.com");
email.send();
System.out.println("Mail sent!");
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它工作得很好,我在我的收件箱中接收電子郵件,但是當我在GWT(Appengine項目)服務器端放置相同的代碼時,它不會工作它顯示沒有錯誤,沒有異常,並說郵件已發送,但它從未實際發送過到我的收件箱
也嘗試了這個
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("appname@appspot.gserviceaccount.com", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("myemail@gmail.com", "Mr. User"));
msg.setSubject("Your Example.com account has been activated");
msg.setText(msgBody);
Transport.send(msg);
} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
請指導我
謝謝
看看這個: Mail Go API概述
並且您沒有收到錯誤,因為沒有錯誤。
郵件已發送,但由於上述限制,您將無法收到郵件。
以下是來自developers.google.com的工作代碼
public String sendMail(String from, String to, String replyTo, String subject, String message) {
String output=null;
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from, "Gmail.com Admin"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(to, "Mr. User"));
msg.setSubject(subject);
msg.setText(message);
msg.setReplyTo(new InternetAddress[]{new InternetAddress(replyTo)});
Transport.send(msg);
} catch (Exception e) {
output=e.toString();
}
return output;
}
通過developers.google.com的引用,發件人地址( from
)必須是以下類型之一:
應用程序的任何有效電子郵件接收地址(例如xxx@APP-ID.appspotmail.com)。
我試過它,它在托管的gwt應用程序中工作。
您是否收到了AddressException並吞下它而沒有顯示錯誤?
我問,因為這是一個不受支持的“來自”地址:
appname@appspot.gserviceaccount.com
它應該是:
anything@appid.appspotmail.com
或管理員或登錄用戶的電子郵件地址。 請參閱此處的規則:
https://developers.google.com/appengine/docs/java/mail/usingjavamail#Senders_and_Recipients
同樣的問題,這適合我,100%也適合你(如果你使用eclipse)。
當您發送郵件時,實際上它已發送到您的localhost服務器。 因此,您必須從App引擎管理控制台創建個人應用程序引擎。
為此,請轉到此鏈接,然后選擇要用作制作應用引擎,創建應用程序的帳戶,而不是編寫應用程序標識符( 注意此應用程序標識符,在部署后在eclipse項目中工作之后 )
然后在應用程序設置中,找到這個已配置的服務 ,因為您必須為您的應用程序授予權限,選擇您的應用程序語言Java / Python / Go,之后您將被重定向到您必須在web.xml中編寫的入站服務
在eclipse之后,右鍵單擊您的項目,轉到Google - > Deploy to app Engine,然后在應用程序ID字段中寫入應用程序ID,該字段就在之前記下。 首先檢查您是否必須安裝應用引擎。
而不是點擊完成。 部署需要一些時間,而不是自動打開瀏覽器。 所以現在你的gwt應用程序在你的服務器上。 請享用 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.