sendgrid java_store-sendgrid-java-how-to-send-email-example | Microsoft Docs

您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

如何在 Azure 部署中通过 Java 使用 SendGrid 发送电子邮件How to Send Email Using SendGrid from Java in an Azure Deployment

10/30/2014

本文内容

以下示例演示如何能够使用 SendGrid 从在 Azure 中托管的网页上发送电子邮件。The following example shows you how you can use SendGrid to send emails from a web page hosted in Azure. 生成的应用程序将提示用户提供电子邮件值,如以下屏幕截图所示。The resulting application will prompt the user for email values, as shown in the following screenshot.

386e458bf237cae4a9c59b43d6c8b73b.png

生成的电子邮件将类似于以下屏幕截图。The resulting email will look similar to the following screenshot.

1e0b96a6c372419a0f4b714df02b6c1e.png

需要执行以下操作来使用本主题中的代码:You'll need to do the following to use the code in this topic:

将这些 JAR 添加到 Java 生成路径。Add the JARs to your Java build path.

如果使用 Eclipse 创建此 Java 应用程序,则可使用 Eclipse 的部署程序集功能在应用程序部署文件 (WAR) 中加入 SendGrid 库。If you are using Eclipse to create this Java application, you can include the SendGrid libraries in your application deployment file (WAR) using Eclipse's deployment assembly feature. 如果不使用 Eclipse 创建此 Java 应用程序,请确保将这些库包含在与 Java 应用程序相同的 Azure 角色中,并将其添加到应用程序的类路径下。If you are not using Eclipse to create this Java application, ensure the libraries are included within the same Azure role as your Java application, and added to the class path of your application.

还必须有自己的 SendGrid 用户名和密码,才能发送电子邮件。You must also have your own SendGrid username and password, to be able to send the email.

此外,强烈建议熟悉在 Eclipse 中为 Azure 创建 Hello World 应用程序上的信息,如果不使用 Eclipse,则强烈建议熟悉在 Azure 中托管 Java 应用程序的其他方法。Additionally, familiarity with the information at Creating a Hello World Application for Azure in Eclipse, or with other techniques for hosting Java applications in Azure if you are not using Eclipse, is highly recommended.

创建用于发送电子邮件的 Web 窗体Create a web form for sending email

以下代码演示如何创建 Web 窗体以检索用于发送电子邮件的用户数据。The following code shows how to create a web form to retrieve user data for sending email. 在此内容中,JSP 文件的名称为 emailform.jsp。For purposes of this content, the JSP file is named emailform.jsp.

pageEncoding="ISO-8859-1" %>

Email form

Fill in all fields and click Send this email.

To:
From:
Subject:
Text:
SendGrid user name:
SendGrid password:

创建用于发送电子邮件的代码Create the code to send the email

以下代码创建电子邮件并发送它,在填写 emailform.jsp 中的窗体时会调用这段代码。The following code, which is called when you complete the form in emailform.jsp, creates the email message and sends it. 在此内容中,JSP 文件的名称为 sendemail.jsp。For purposes of this content, the JSP file is named sendemail.jsp.

pageEncoding="ISO-8859-1" import="javax.activation.*, javax.mail.*, javax.mail.internet.*, java.util.Date, java.util.Properties" %>

Email processing happens here

This is my send mail page.

final String sendGridUser = request.getParameter("sendGridUser");

final String sendGridPassword = request.getParameter("sendGridPassword");

class SMTPAuthenticator extends Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

String username = sendGridUser;

String password = sendGridPassword;

return new PasswordAuthentication(username, password);

}

}

try

{

// The SendGrid SMTP server.

String SMTP_HOST_NAME = "smtp.sendgrid.net";

Properties properties;

properties = new Properties();

// Specify SMTP values.

properties.put("mail.transport.protocol", "smtp");

properties.put("mail.smtp.host", SMTP_HOST_NAME);

properties.put("mail.smtp.port", 587);

properties.put("mail.smtp.auth", "true");

// Display the email fields entered by the user.

out.println("Value entered for email Subject: " + request.getParameter("emailSubject") + "
");

out.println("Value entered for email To: " + request.getParameter("emailTo") + "
");

out.println("Value entered for email From: " + request.getParameter("emailFrom") + "
");

out.println("Value entered for email Text: " + "
" + request.getParameter("emailText") + "
");

// Create the authenticator object.

Authenticator authenticator = new SMTPAuthenticator();

// Create the mail session object.

Session mailSession;

mailSession = Session.getDefaultInstance(properties, authenticator);

// Display debug information to stdout, useful when using the

// compute emulator during development.

mailSession.setDebug(true);

// Create the message and message part objects.

MimeMessage message;

Multipart multipart;

MimeBodyPart messagePart;

message = new MimeMessage(mailSession);

multipart = new MimeMultipart("alternative");

messagePart = new MimeBodyPart();

messagePart.setContent(request.getParameter("emailText"), "text/html");

multipart.addBodyPart(messagePart);

// Specify the email To, From, Subject and Content.

message.setFrom(new InternetAddress(request.getParameter("emailFrom")));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("emailTo")));

message.setSubject(request.getParameter("emailSubject"));

message.setContent(multipart);

// Uncomment the following if you want to add a footer.

// message.addHeader("X-SMTPAPI", "{\"filters\": {\"footer\": {\"settings\": {\"enable\":1,\"text/html\": \"This is my email footer.\"}}}}");

// Uncomment the following if you want to enable click tracking.

// message.addHeader("X-SMTPAPI", "{\"filters\": {\"clicktrack\": {\"settings\": {\"enable\":1}}}}");

Transport transport;

transport = mailSession.getTransport();

// Connect the transport object.

transport.connect();

// Send the message.

transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));

// Close the connection.

transport.close();

out.println("

Email processing completed.

");

}

catch (Exception e)

{

out.println("

Exception encountered: " +

e.getMessage() +

"

");

}

%>

除了发送电子邮件外,emailform.jsp 还为用户提供了结果;下面的屏幕截图是一个示例:In addition to sending the email, emailform.jsp provides a result for the user; an example is the following screenshot:

17cc40aa09b81d1816e9ae4f3da3da09.png

后续步骤Next steps

将应用程序部署到计算模拟器,然后在浏览器内运行 emailform.jsp,在窗体中输入值,单击“发送此电子邮件”,然后在 sendemail.jsp 中查看结果。Deploy your application to the compute emulator and within a browser run emailform.jsp, enter values in the form, click Send this email, and then see results in sendemail.jsp.

提供这段代码是为了向你演示如何在 Azure 上通过 Java 使用 SendGrid。This code was provided to show you how to use SendGrid in Java on Azure. 在生产中部署到 Azure 之前,可能希望添加更多错误处理或其他功能。Before deploying to Azure in production, you may want to add more error handling or other features. 例如:For example:

你可以使用 Azure 存储 BLOB 或 SQL 数据库(而不是使用 Web 窗体)存储电子邮件地址和电子邮件。You could use Azure storage blobs or SQL Database to store email addresses and email messages, instead of using a web form. 有关通过 Java 使用 Azure Blob 存储 Blob 的信息,请参阅如何从 Java 使用 Azure Blob 存储服务。For information about using Azure storage blobs in Java, see How to Use the Blob Storage Service from Java. 若要了解如何在 Java 中使用 SQL 数据库,请参阅在 Java 中使用 SQL 数据库。For information about using SQL Database in Java, see Using SQL Database in Java.

若要了解如何在 Java 中使用 SendGrid,请参阅如何通过 Java 使用 SendGrid 发送电子邮件。For more information about using SendGrid in Java, see How to send email using SendGrid from Java.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值