【问题标题】:How do we send an SMTP email using AWS from my JAVA (Android studio) app我们如何从我的 JAVA (Android studio) 应用程序使用 AWS 发送 SMTP 电子邮件
【发布时间】:2023-02-03 15:38:32
【问题描述】:

我怀疑这是我的 transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);线。我认为根本没有建立任何联系。我的代码目前看起来像这样:

private void sendEmail(String messegeToSend) {

    final String FROM = "joe.blogs@mydomain.com";
    final String FROMNAME = "Joe Blogs";
    final String TO = "joe.blogs@joeblogs.com";
    final String HOST = "email-smtp.us-west-2.amazonaws.com";
    final int PORT = 587;
    final String SMTP_USERNAME = "smtpusername";
    final String SMTP_PASSWORD = "smtppassword";

    try {
        Properties props = System.getProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(FROM));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(TO));
        message.setSubject("InvoiceRequest");
        message.setText(messegeToSend);
        Transport transport = session.getTransport();

        transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);

        Toast.makeText(getApplicationContext(),"Connected!",Toast.LENGTH_LONG).show();

        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }catch (MessagingException e){
        Toast.makeText(getApplicationContext(),"Sorry, We ran into a problem"+ e.getMessage(),Toast.LENGTH_LONG).show();
        throw  new RuntimeException(e);
    }
}

最后,我想最终从这个应用程序中发送一封电子邮件。我以前使用的是 Google 的 Gmail SMTP,但它即将停用,因此我已切换到 AWS SES,我现在正在努力使用它。

【问题讨论】:

  • 我不知道您为什么认为 GMail SMTP 已停用(不是),但您遇到了什么错误?您确定要将用户名/密码等凭据放入您的应用程序中吗?如果有人反编译了您的应用程序,他们可以像您一样发送电子邮件。

标签: java android amazon-web-services smtp gmail-imap


【解决方案1】:

也遇到过同样的问题。

网络操作,如 transport.connect(),也必须在线程中运行!在主线程上发出网络请求将导致线程等待或阻塞。因此,您的解决方案的问题是将 AmazonSESSample 类扩展到 Thread 类。

第1步)我假设你有按照 Amazon 的 AWS 文档中提供的步骤通过 Amazon SES SMTP 接口以编程方式发送电子邮件.如果不是这里是它的链接:here

第2步)下载并导入以下 jar 文件(如果您尚未这样做):

https://drive.google.com/drive/folders/1q5n2ROQvlmvkW7DAWyhGxzceRustouhK?usp=sharing

步骤 3)而不是遵循他们提供的代码,这是有效的修改版本:

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@RequiresApi(api = Build.VERSION_CODES.O)
public class AmazonSESSample extends Thread{

    // Replace sender@example.com with your "From" address.
    // This address must be verified.
    static final String FROM = "sender@example.com";
    static final String FROMNAME = "Sender Name";
    
    // Replace recipient@example.com with a "To" address. If your account 
    // is still in the sandbox, this address must be verified.
    static final String TO = "recipient@example.com";
    
    // Replace smtp_username with your Amazon SES SMTP user name.
    static final String SMTP_USERNAME = "smtp_username";
    
    // Replace smtp_password with your Amazon SES SMTP password.
    static final String SMTP_PASSWORD = "smtp_password";
    
    // The name of the Configuration Set to use for this message.
    // If you comment out or remove this variable, you will also need to
    // comment out or remove the header below.
    static final String CONFIGSET = "ConfigSet";
    
    // Amazon SES SMTP host name. This example uses the US West (Oregon) region.
    // See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints
    // for more information.
    static final String HOST = "email-smtp.us-west-2.amazonaws.com";
    
    // The port you will connect to on the Amazon SES SMTP endpoint. 
    static final int PORT = 587;
    
    static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";
    
    static final String BODY = String.join(
            System.getProperty("line.separator"),
            "<h1>Amazon SES SMTP Email Test</h1>",
            "<p>This email was sent with Amazon SES using the ", 
            "<a href='https://github.com/javaee/javamail'>Javamail Package</a>",
            " for <a href='https://www.java.com'>Java</a>."
        );

    public void run() {

        // Create a Properties object to contain connection configuration information.
        Properties props = System.getProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", PORT); 
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");

        // Create a Session object to represent a mail session with the specified properties. 
        Session session = Session.getDefaultInstance(props);

        // Create a message with the specified information. 
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM,FROMNAME));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setContent(BODY,"text/html");
        
        // Add a configuration set header. Comment or delete the 
        // next line if you are not using a configuration set
        msg.setHeader("X-SES-CONFIGURATION-SET", CONFIGSET);
            
        // Create a transport.
        Transport transport = session.getTransport();
                    
        // Send the message.
        try
        {
            System.out.println("Sending...");
            
            // Connect to Amazon SES using the SMTP username and password you specified above.
            transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
            
            // Send the email.
            transport.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Email sent!");
        }
        catch (Exception ex) {
            System.out.println("The email was not sent.");
            System.out.println("Error message: " + ex.getMessage());
        }
        finally
        {
            // Close and terminate the connection.
            transport.close();
        }
    }
}

重要的是您也将以下 jar 库导入到您的项目中:

  1. javax.activation.jar
  2. javax.additionnal.jar
  3. javax.mail.jar

【讨论】:

    猜你喜欢
    • 2015-05-06
    • 2011-01-12
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多