我想通過GMail的SMTP服務器從jsp頁面發送電子郵件,但我沒有得到正確的結果如何從jsp頁面使用GMail SMTP服務器發送電子郵件?
0
A
回答
0
您必須使用Java Mail API。
3
從JSP開始,當您提交表單時,您需要調用一個Java類,在其中編寫使用JavaMail發送郵件的代碼。你可以在這裏找到一個例子 http://code.geekinterview.com/java/send-email-using-gmail-mail-server.html:
//set CLASSPATH=%CLASSPATH%;activation.jar;mail.jar
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Mail
{
String d_email = "[email protected]",
d_password = "****",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "[email protected]",
m_subject = "Testing",
m_text = "Hey, this is the testing email using smtp.gmail.com.";
public static void main(String[] args)
{
String[] to={"[email protected]"};
String[] cc={"[email protected]"};
String[] bcc={"[email protected]"};
//This is for google
Mail.sendMail("[email protected]","password","smtp.gmail.com","465","true",
"true",true,"javax.net.ssl.SSLSocketFactory","false",to,cc,bcc,
"hi baba don't send virus mails..","This is my style...of reply..
If u send virus mails..");
}
public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String[] to,String[] cc,String[] bcc,String subject,String text){
Properties props = new Properties();
//Properties props=System.getProperties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if(!"".equals(port))
props.put("mail.smtp.port", port);
if(!"".equals(starttls))
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
if(debug){
props.put("mail.smtp.debug", "true");
}else{
props.put("mail.smtp.debug", "false");
}
if(!"".equals(port))
props.put("mail.smtp.socketFactory.port", port);
if(!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
if(!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("[email protected]"));
for(int i=0;i<to.length;i++){
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
}
for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
}
for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
}
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
catch (Exception mex)
{
mex.printStackTrace();
return false;
}
}
}
相關問題
- 1. 不能發送電子郵件從jsp使用Gmail的SMTP服務器
- 2. 無法使用codeIgniter從gmail smtp服務器發送電子郵件?
- 3. 如何使用SMTP服務器發送電子郵件?
- 4. 使用本地SMTP服務器從Azure發送電子郵件
- 5. 使用GMail的SMTP服務器從PHP頁面發送郵件的問題
- 6. 如何從我的SMTP服務器發送電子郵件?
- 7. 從本地smtp服務器發送電子郵件node.js
- 8. 與Gmail的SMTP發送電子郵件
- 9. 使用SMTP服務器通過電子郵件發送附件
- 10. 使用Gmail的SMTP服務器每天發送超過1000封電子郵件
- 11. 使用SSL連接通過Gmail SMTP服務器發送電子郵件通知
- 12. 服務器使用Gmail發送電子郵件smtp獲取提醒
- 13. 錯誤的電子郵件使用Gmail發送SMTP服務器CDOSYS
- 14. 使用您的Gmail帳戶發送電子郵件作爲SMTP服務器
- 15. 從未啓用SMTP的服務器發送電子郵件
- 16. 如何使用Gmail的SMTP和Indy 10發送電子郵件?
- 17. ASP.NET C# - 從Gmail/Yahoo發送電子郵件實時SMTP服務器不工作
- 18. 發送SMTP的Gmail電子郵件'發送郵件失敗'
- 19. 無法從SMTP服務器發送電子郵件
- 20. 使用asp.net和谷歌smtp服務器發送電子郵件
- 21. 無法使用服務器上的SMTP發送電子郵件
- 22. iphone使用smtp服務器發送電子郵件?
- 23. 如何通過其他服務器發送SMTP電子郵件?
- 24. 如何使用Google smtp服務器從django發送電子郵件?
- 25. 在Windows服務器上使用Gmail發送電子郵件2008
- 26. 如何使用gmail smtp服務發送郵件?
- 27. 可以appengine從外部SMTP服務器發送電子郵件?
- 28. 測試發送電子郵件從SharePoint沒有smtp服務器
- 29. 從SMTP遠程服務器發送電子郵件CakePHP 3
- 30. 從SMTP服務器發送帶有PHP的電子郵件
返回的結果是......? – Raptor