使用Javamail发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Javamail发送电子邮件相关的知识,希望对你有一定的参考价值。

我正在尝试使用Javamail发送电子邮件,因为它似乎是使用SMTP发送电子邮件的唯一方法。

我搜索了一些帖子,如:Sending mail in android without intents using SMTP

但是,我希望发件人成为我自己的域名,而不是gmail。

所以我尝试过:

    final String username = "admin@customsite.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.customsite.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("admin@customsite.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("username@gmail.com"));
            message.setSubject("Hello there");
            message.setText("This is a test email");
            Transport.send(message);
            Log.d("EmailClient", "Success");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
    }

但它似乎没有奏效。

答案

一周前我做过同样的事情,这里有自定义smtp,这是我的代码

类SendMail.JAVA,我在Config.JAVA中有密码和邮件帐户,请注意我的服务器在端口25上运行并且没有SSL

public class SendMail extends AsyncTask<Void,Void,Void> {

//Declaring Variables
private Context context;
private Session session;

//Information to send email
private String email;
private String subject;
private String message;

//Progressdialog to show while sending email
private ProgressDialog progressDialog;

//Class Constructor
public SendMail(Context context, String email, String subject, String message){
    //Initializing variables
    this.context = context;
    this.email = email;
    this.subject = subject;
    this.message = message;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    //Showing progress dialog while sending email
    progressDialog = ProgressDialog.show(context,"Enviando mail","Espere por favor...",false,false);
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Dismissing the progress dialog
    progressDialog.dismiss();
    //Showing a success message
    Toast.makeText(context,"Mail enviado",Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... params) {
    //Creating properties
    Properties props = new Properties();

    //Configuring properties for gmail
    //If you are not using gmail you may need to change the values
    props.put("mail.smtp.host", "Custom SMTP");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "25");

    //Creating a new session
    session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                //Authenticating the password
                protected PasswordAuthentication getPasswordAuthentication() {

                    return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
                }
            });

    try {
        //Creating MimeMessage object
        MimeMessage mm = new MimeMessage(session);

        //Setting sender address
        mm.setFrom(new InternetAddress(Config.EMAIL));
        //Adding receiver
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //Adding subject
        mm.setSubject(subject);
        //Adding message
        mm.setText(message, "utf-8", "html");

        //Sending email
        Transport.send(mm);

    } catch (MessagingException e) {
        e.printStackTrace();
    }
    return null;
}

}

Activity中的示例实现

private void sendEmail() {
        //Getting content for email
        String email = asd.getText().toString();
        String subject = "Presupuesto plaza hogar";
        String message = body.toString();

        //Creating SendMail object
        SendMail sm = new SendMail(this, email, subject, message);

        //Executing sendmail to send email
        sm.execute();
    }

以上是关于使用Javamail发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

JavaMail:利用JavaMail发送复杂邮件

javamail电子邮件不发送

如何使用 JavaMail 发送带有附件的 html 电子邮件

如何使用 JavaMail 发送 html 电子邮件?

Spring集成JavaMail并利用线程池发送邮件

使用 javamail API 发送带有附件的电子邮件