使用 Java 邮件哪个更安全
Which is more secure using Java Mail
我已经编写了使用 JavaMail 发送电子邮件的代码,并且很想了解以下两个选项中哪一个更安全且更好。两组代码目前都可以使用 Gmail、Outlook 或 Yahoo 通过 SMTP 发送。代码来自 Android,但更通用 Java.
String password = "**********";
String username = "????@gmail.com";//"????@outlook.com";//"????@yahoo.com";
String smtp_host_setting = "smtp.gmail.com";//"smtp-mail.outlook.com";//"smtp.mail.yahoo.com";
String smtp_port_setting = "587";//"587";//"587";
String recipient_email_address = "recipient@recipient_server.com";
String email_subject = "Email Subject";
String email_msg = "Some text for the message\r\nThanks!";
Session session = null;
此方法使用 getPasswordAuthentication() :
/************************ OPTION 1 *****************************/
private int send_email_temp(){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtp_host_setting); // YAHOO needs it to be mail.smtp.host, GMAIL and OUTLOOK were OK with mail.host (and with this)
//props.put("mail.debug", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", smtp_port_setting);
session = Session.getInstance(
props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
ActuallySendAsync_temp asy = new ActuallySendAsync_temp(true);
asy.execute();
return 0;
}
class ActuallySendAsync_temp extends AsyncTask<String, String, Void> {
public ActuallySendAsync_temp(boolean boo) {
// something to do before sending email
}
@Override
protected Void doInBackground(String... params) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient_email_address));
message.setSubject(email_subject);
message.setText(email_msg);
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// something to do after sending email
}
}
/************************ OPTION 1 - End ***********************/
此方法使用 session.getTransport("smtp") 和 .connect 进行身份验证:
/************************ OPTION 2 *****************************/
private int send_email_temp(){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtp_host_setting); // YAHOO needs it to be mail.smtp.host, GMAIL and OUTLOOK were OK with mail.host (and with this)
//props.put("mail.debug", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", smtp_port_setting);
session = Session.getInstance(props);
ActuallySendAsync_temp asy = new ActuallySendAsync_temp(true);
asy.execute();
return 0;
}
class ActuallySendAsync_temp extends AsyncTask<String, String, Void> {
public ActuallySendAsync_temp(boolean boo) {
// something to do before sending email
}
@Override
protected Void doInBackground(String... params) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient_email_address));
message.setSubject(email_subject);
message.setText(email_msg);
Transport transport = session.getTransport("smtp");
transport.connect(smtp_host_setting, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// something to do after sending email
}
}
/************************ OPTION 2 - End ***********************/
这两种方法是否等效,或者一个选项被认为比另一个更安全?
感谢您的帮助。
它们同样安全。一个只是更冗长。
我已经编写了使用 JavaMail 发送电子邮件的代码,并且很想了解以下两个选项中哪一个更安全且更好。两组代码目前都可以使用 Gmail、Outlook 或 Yahoo 通过 SMTP 发送。代码来自 Android,但更通用 Java.
String password = "**********";
String username = "????@gmail.com";//"????@outlook.com";//"????@yahoo.com";
String smtp_host_setting = "smtp.gmail.com";//"smtp-mail.outlook.com";//"smtp.mail.yahoo.com";
String smtp_port_setting = "587";//"587";//"587";
String recipient_email_address = "recipient@recipient_server.com";
String email_subject = "Email Subject";
String email_msg = "Some text for the message\r\nThanks!";
Session session = null;
此方法使用 getPasswordAuthentication() :
/************************ OPTION 1 *****************************/
private int send_email_temp(){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtp_host_setting); // YAHOO needs it to be mail.smtp.host, GMAIL and OUTLOOK were OK with mail.host (and with this)
//props.put("mail.debug", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", smtp_port_setting);
session = Session.getInstance(
props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
ActuallySendAsync_temp asy = new ActuallySendAsync_temp(true);
asy.execute();
return 0;
}
class ActuallySendAsync_temp extends AsyncTask<String, String, Void> {
public ActuallySendAsync_temp(boolean boo) {
// something to do before sending email
}
@Override
protected Void doInBackground(String... params) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient_email_address));
message.setSubject(email_subject);
message.setText(email_msg);
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// something to do after sending email
}
}
/************************ OPTION 1 - End ***********************/
此方法使用 session.getTransport("smtp") 和 .connect 进行身份验证:
/************************ OPTION 2 *****************************/
private int send_email_temp(){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtp_host_setting); // YAHOO needs it to be mail.smtp.host, GMAIL and OUTLOOK were OK with mail.host (and with this)
//props.put("mail.debug", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", smtp_port_setting);
session = Session.getInstance(props);
ActuallySendAsync_temp asy = new ActuallySendAsync_temp(true);
asy.execute();
return 0;
}
class ActuallySendAsync_temp extends AsyncTask<String, String, Void> {
public ActuallySendAsync_temp(boolean boo) {
// something to do before sending email
}
@Override
protected Void doInBackground(String... params) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient_email_address));
message.setSubject(email_subject);
message.setText(email_msg);
Transport transport = session.getTransport("smtp");
transport.connect(smtp_host_setting, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// something to do after sending email
}
}
/************************ OPTION 2 - End ***********************/
这两种方法是否等效,或者一个选项被认为比另一个更安全?
感谢您的帮助。
它们同样安全。一个只是更冗长。