首先你身份進行驗證發件人的,然後在你的代碼發送電子郵件這樣的使用javax.mail
props.put("mail.smtp.user", "[email protected]");
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress("[email protected]"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
Multipart multipart = new MimeMultipart();
String data=" Hello There.";
//attach Image
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(pathUserImg);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("IMG_NAME.png");
//attach String Data
MimeBodyPart的messageBodyPart1 =新MimeBodyPart的(); messageBodyPart1.setText(「data:」+ data);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPart1);
msg.setContent(multipart);
Transport.send(msg);
} catch (Exception mex)
{
mex.printStackTrace();
Log.v("TAG", " Email Not Sent "+mex.getMessage());
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication([email protected]@gmail.com", "YourPASSWORD");
}
}
感謝您的評論,我正在按照發送郵件的確切程序,因爲我在我的帖子中提到它不工作。 – 2014-09-29 11:58:10
可以把你的代碼放在這裏?那麼我可以嘗試找出什麼是錯的 – 2014-09-29 11:59:35
還檢查權限 – 2014-09-29 12:00:14