我们如何使用AWS从我的JAVA (Android studio)应用程序发送SMTP电子邮件



我怀疑是我的交通工具。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);
}
}

在这一切结束时,我想最后从这个应用程序内发送电子邮件。我以前使用谷歌的Gmail SMTP,但它是退役,因此我已经切换到AWS SES,我现在正在挣扎。

我也遇到过同样的问题。

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

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

步骤2)如果您还没有下载并导入以下jar文件:

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

步骤3)将以下实现添加到您的构建中。gradle文件:

dependencies{
implementation files('libs\javax.mail.jar')
implementation files('libs\javax.activation.jar')
implementation files('libs\javax.additionnal.jar')
}

第4步)不遵循他们提供的代码,这是修改后的版本:

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

相关内容

  • 没有找到相关文章

最新更新