Java ssl电子邮件发送到垃圾邮件而非收件箱

时间:2015-09-01 07:33:54

标签: java email

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendEmail {

private static String hostname;
private static String username;
private static String password;
private static String sendTo;
private static String sentFrom;
private static String emailBody;

// sendSLLEmail()
public String sendSLLEmail(String hostname, String username,
String password, String sendTo, String sentFrom,String subject, String     emailBody) {
try {
SendEmail.hostname = hostname;
SendEmail.username = username;
SendEmail.password = password;
SendEmail.sendTo = sendTo;
SendEmail.sentFrom = sentFrom;
SendEmail.emailBody = emailBody;

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SendEmail.hostname);
props.put("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setContent(SendEmail.emailBody, "text/html; charset=utf-8");
message.setFrom(new InternetAddress(SendEmail.sentFrom));
message.addRecipient(Message.RecipientType.TO, new     InternetAddress(SendEmail.sendTo));

transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
return "1";
} catch (Exception e) {
e.printStackTrace();
return "-1";
}
}

private class SMTPAuthenticator extends javax.mail.Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        String usernameTest = SendEmail.username;
        String passwordTest = SendEmail.password;
        return new PasswordAuthentication(usernameTest, passwordTest);
    }
}

}

当我使用此代码时,我将值作为参数传递给它。电子邮件发送并运行良好,但总是登陆我的客户垃圾邮件文件夹,任何人都可以告诉我如何防止这种情况?

当我运行此代码时,我使用自己的邮件服务器,因此我会将所有正确的参数传递给代码,然后将其连接到我的邮件服务器,然后邮寄到垃圾邮件。

0 个答案:

没有答案