我在從我的應用程序發送郵件時遇到問題。我無法在我的應用程序上使用簡單的SMTP服務器。並不知道如何處理在JAVA中發送郵件。我應該使用PHP的mail()使用相同/類似的機制。不幸的是,我不知道該怎麼做。如何使用類似於PHP郵件中的相同機制以Java方式發送郵件
2
A
回答
2
您需要查看JavaMail API,並且由於PHP的mail()需要,它需要一個SMTP服務器來發送該電子郵件。
如果您需要SMTP服務器,建議您在Google上搜索適用於您的操作系統的SMTP服務器,或者您可以使用ISP或服務器主機提供的SMTP服務器。
5
Java Mail API支持發送和接收電子郵件。該API提供了一種插件架構,供應商可以在運行時動態發現和使用其專有協議的實施。 Sun提供的參考實現和其支持以下協議:
- 互聯網郵件訪問協議(IMAP)
- 簡單郵件傳輸協議(SMTP)
- 郵局協議3(POP 3)
下面是如何使用它的一個例子:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
private String from;
private String to;
private String subject;
private String text;
public SendMail(String from, String to, String subject, String text){
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
}
public static void main(String[] args) {
String from = "[email protected]";
String to = "[email protected]";
String subject = "Test";
String message = "A test message";
SendMail sendMail = new SendMail(from, to, subject, message);
sendMail.send();
}
public void send(){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
e.printStackTrace();
}
try {
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
Transport.send(simpleMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
0
您可以使用JavaMail API與本地像Apache James SMTP服務器,因此安裝和運行James服務器後,您可以在SMTP服務器IP設置爲127.0.0.1
0
我應該使用相同/相似的機制,如PHP的郵件()使用。
你不能,因爲它不存在。如果這是要求,那就改變它。
不幸的是,我不知道該怎麼做。
請參閱JavaMail API。
相關問題
- 1. 使用java郵件API發送郵件
- 2. 如何使用php的郵件發送郵件()反映在發件人郵箱的「已發送郵件」
- 3. 用於PHP的PHP IIS的PHP郵件()發送垃圾郵件
- 4. PHP發送郵件複製機構
- 5. 發送郵件(Java vs Php)
- 6. 如何使用@ outlook.com的Java郵件發送電子郵件?
- 7. 如何以與我可以在Windows資源管理器中「發送到郵件收件人」相同的方式以編程方式發送電子郵件?
- 8. 發送郵件使用PHP
- 9. 使用php發送郵件
- 10. 使用php發送郵件
- 11. 如何使用PHP的郵件()函數發送電子郵件
- 12. 以PHP發送HTML郵件
- 13. 使用PHP發送郵件(將電子郵件發送到EMAIL INPUT的郵件)
- 14. 如何以Java發送電子郵件?
- 15. 如何使用java將郵件發送到郵件列表?
- 16. 如何使用java郵件從android發送電子郵件
- 17. 用PHP郵件發送電子郵件()
- 18. 如何發送圖像郵件使用PHP郵件功能
- 19. 使用其他方式發送郵件
- 20. 如何以HTML格式發送郵件?
- 21. 以編程方式發送郵件Android
- 22. Java發送郵件
- 23. 通過Java API發送郵件:即使附件失敗,如何發送郵件?
- 24. 如何使用php發送本地主機的郵件CodeIgniter
- 25. 用java發送郵件(我正在使用java郵件API)
- 26. 發送PHP的使用PHP郵件
- 27. 使用crashmail通過smtp以超類相關方式發送電子郵件
- 28. 如何使用Java郵件API保存已發郵件郵件?
- 29. 用PHP發送html格式的郵件
- 30. 發送郵件php
我不能只使用SMTP發送郵件。我需要一些類似於PHP的機制,它使用sendmail系統發送郵件。 – J33nn 2013-02-10 23:28:13
但是PHP的mail()確實使用了SMTP。 – 2013-02-10 23:31:42
您能告訴我哪裏可以找到PHP郵件功能使用的STMP配置嗎?這是在php.ini文件中配置的權利? – J33nn 2013-02-10 23:36:51