在本文中,我们将发送一封电子邮件,其中包含精心设计的表单以显示与邮件一起发送的数据。
众所周知,对我们所有人来说,使用电子邮件已经变得十分必要和重要。有各种电子邮件服务提供商,例如Yahoo,Outlook,iCloud和Gmail等。这些技术巨头为每个用户提供免费的电子邮件服务。如果您使用互联网,则无法忽略它,因为每当您访问任何网站/应用程序以在线购买/销售产品时,都可以。首先,您必须在这些网站/应用程序上注册才能证明您在现实世界中的身份。
在本文中,我们将学习如何使用JavaMail API将Gmail收件箱中的电子邮件发送到指定的电子邮件地址。我们将发送常见的用户数据,例如用户名,电子邮件地址,手机号码。等等
因此,让我们开始吧。
步骤1
- 首先,创建空的android项目,并在根目录中创建名称为MailSender.java和JSSEProvider.java的Java类。
- 现在,我们需要将Java Mail API jar文件导入到我们的项目中。从此处下载jar文件,解压缩并将其粘贴到libs文件夹中。
- 如果您的项目无法识别您的库,请重新启动Android Studio。
- 右键单击app-> New-> Folder-> Assets Folder,创建资产文件夹。我们将HTML文件放在此处,以在Gmail上显示数据。
- 要发送电子邮件,您的应用将需要互联网连接,因此请在AndroidManifest.xml文件中声明此权限。
<uses-permission android:name="android.permission.INTERNET"/>
第2步
- 在MailSender.java类中创建一些实例变量
// change this host name accordingly private String mailhost = "webmail.xyz.in"; private String user; private String password; private Session session; Context context; private Multipart _multipart = new MimeMultipart();
- 现在,在MailSender.java文件中创建静态块以调用JSSEProvider.java类。JSSEProvider用于链接SSLContext,X509的KeyManagerFactory,X509的TrustManagerFactory和AndroidCAstore的修改版本。
static { Security.addProvider(new JSSEProvider()); }
- 创建参数化的构造函数(传递上下文,“发件人电子邮件ID”和“密码”等)以初始化实例变量,并创建Properties类的对象以在构造函数内设置属性。在其中传递上下文,用户电子邮件ID和密码。看一下构造函数代码。
public MailSender(Context context,String user, String password) { this.user = user; this.password = password; this.context = context; Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", mailhost); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.quitwait", "false"); session = Session.getDefaultInstance(props, this); }
- 创建密码验证器方法以验证您的帐户。
protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); }
- 最后,创建用户定义的方法sendUserDetailWithImage()以将数据发送到邮件。下面的代码从assets文件夹中提取user_profile.html,将其转换为缓冲区,然后从缓冲区中读取它,最后将其转换为字符串对象,用实际的用户名替换关键字“ ”等等。请参阅完整的sendUserDetailWithImage()代码。
public synchronized void sendUserDetailWithImage(String subject, String body, String sender, String recipients,String username,String email,String mobile,String dob,String age,String address,String profilePic ) throws Exception { MimeMessage message = new MimeMessage(session); DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); message.setFrom(new InternetAddress("no-reply@astromyntra.in")); message.setSender(new InternetAddress(sender)); message.setSubject(subject); message.setDataHandler(handler); BodyPart messageBodyPart = new MimeBodyPart(); InputStream is = context.getAssets().open("user_profile.html"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); String str = new String(buffer); str =str.replace("$headermessage$","You have got a new user."); str=str.replace("$username$", username); str=str.replace("$email$", email); str=str.replace("$mobile$", mobile); str=str.replace("$dob$",dob); str=str.replace("$age$",age); str=str.replace("$address$", address); messageBodyPart.setContent(str,"text/html; charset=utf-8"); _multipart.addBodyPart(messageBodyPart); // Put parts in message message.setContent(_multipart); if (recipients.indexOf(',') > 0) message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); else message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); Transport.send(message); }
UserDataSender.java
看完整的MainSender.java代码
package com.emailsender; import android.content.Context; import java.io.InputStream; import java.security.Security; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.util.ByteArrayDataSource; public class MailSender extends javax.mail.Authenticator { // change this host name private String mailhost = "webmail.xyz.in"; private String user; private String password; private Session session; Context context; private Multipart _multipart = new MimeMultipart(); static { Security.addProvider(new JSSEProvider()); } public MailSender(Context context,String user, String password) { this.user = user; this.password = password; this.context = context; Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", mailhost); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.quitwait", "false"); session = Session.getDefaultInstance(props, this); } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } public synchronized void sendUserDetailWithImage(String subject, String body, String sender, String recipients,String username,String email,String mobile,String dob,String age,String address,String profilePic ) throws Exception { MimeMessage message = new MimeMessage(session); DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); message.setFrom(new InternetAddress("no-reply@xyz.in")); message.setSender(new InternetAddress(sender)); message.setSubject(subject); message.setDataHandler(handler); BodyPart messageBodyPart = new MimeBodyPart(); InputStream is = context.getAssets().open("user_profile.html"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); String str = new String(buffer); str =str.replace("$headermessage$","You have got a new user."); str=str.replace("$username$", username); str=str.replace("$email$", email); str=str.replace("$mobile$", mobile); str=str.replace("$dob$",dob); str=str.replace("$age$",age); str=str.replace("$address$", address); messageBodyPart.setContent(str,"text/html; charset=utf-8"); _multipart.addBodyPart(messageBodyPart); message.setContent(_multipart); if (recipients.indexOf(',') > 0) message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); else message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); Transport.send(message); } }
MailSender.java
第三步
要在表单中显示数据,请在assets文件夹内创建名为user_profile.html的HTML文件,然后粘贴以下代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title></title> <style type="text/css">@media only screen and (max-width:480px){body,table,td,p,a,li,blockquote{-webkit-text-size-adjust:none !important}body{width:100% !important;min-width:100% !important}td[id=bodyCell]{padding:10px !important}table.kmMobileHide{display:none !important}table[class=kmTextContentContainer]{width:100% !important}table[class=kmBoxedTextContentContainer]{width:100% !important}td[class=kmImageContent]{padding-left:0 !important;padding-right:0 !important}img[class=kmImage]{width:100% !important}td.kmMobileStretch{padding-left:0 !important;padding-right:0 !important}table[class=kmSplitContentLeftContentContainer],table[class=kmSplitContentRightContentContainer],table[class=kmColumnContainer],td[class=kmVerticalButtonBarContentOuter] table[class=kmButtonBarContent],td[class=kmVerticalButtonCollectionContentOuter] table[class=kmButtonCollectionContent],table[class=kmVerticalButton],table[class=kmVerticalButtonContent]{width:100% !important}td[class=kmButtonCollectionInner]{padding-left:9px !important;padding-right:9px !important;padding-top:9px !important;padding-bottom:0 !important;background-color:transparent !important}td[class=kmVerticalButtonIconContent],td[class=kmVerticalButtonTextContent],td[class=kmVerticalButtonContentOuter]{padding-left:0 !important;padding-right:0 !important;padding-bottom:9px !important}table[class=kmSplitContentLeftContentContainer] td[class=kmTextContent],table[class=kmSplitContentRightContentContainer] td[class=kmTextContent],table[class=kmColumnContainer] td[class=kmTextContent],table[class=kmSplitContentLeftContentContainer] td[class=kmImageContent],table[class=kmSplitContentRightContentContainer] td[class=kmImageContent]{padding-top:9px !important}td[class="rowContainer kmFloatLeft"],td.rowContainer.kmFloatLeft,td[class="rowContainer kmFloatLeft firstColumn"],td.rowContainer.kmFloatLeft.firstColumn,td[class="rowContainer kmFloatLeft lastColumn"],td.rowContainer.kmFloatLeft.lastColumn{float:left;clear:both;width:100% !important}table[class=templateContainer],table[class="templateContainer brandingContainer"],table[class=templateRow]{max-width:600px !important;width:100% !important}h1{font-size:40px !important;line-height:130% !important}h2{font-size:32px !important;line-height:130% !important}h3{font-size:24px !important;line-height:130% !important}h4{font-size:18px !important;line-height:130% !important}td[class=kmTextContent]{font-size:14px !important;line-height:130% !important}td[class=kmTextBlockInner] td[class=kmTextContent]{padding-right:18px !important;padding-left:18px !important}table[class="kmTableBlock kmTableMobile"] td[class=kmTableBlockInner]{padding-left:9px !important;padding-right:9px !important}table[class="kmTableBlock kmTableMobile"] td[class=kmTableBlockInner] [class=kmTextContent]{font-size:14px !important;line-height:130% !important;padding-left:4px !important;padding-right:4px !important}} .tdwidth { width:30%; } .kmTable th { text-align: left !important; padding-left: 40px !important; color: #cd0908 !important; } .kmTable td { text-align: left !important; padding-left: 40px !important; } .kmTable th:nth-child(2) { padding-left: 10px !important; } .kmTable td:nth-child(2) { padding-left: 10px !important; } </style> </head> <body style="margin:0;padding:0;background-color:#eee"> <center> <table align="center" border="0" cellpadding="0" cellspacing="0" id="bodyTable" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding:0;background-color:#eee;height:100%;margin:0;width:100%"> <tbody> <tr> <td align="center" id="bodyCell" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding-top:50px;padding-left:20px;padding-bottom:20px;padding-right:20px;border-top:0;height:100%;margin:0;width:100%"> <table border="0" cellpadding="0" cellspacing="0" class="templateContainer" width="600" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;border:0 none #aaa;background-color:#fff;border-radius:0"> <tbody> <tr> <td class="templateContainerInner" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding:0"> <table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tr> <td align="center" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <table border="0" cellpadding="0" cellspacing="0" class="templateRow" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody> <tr> <td class="rowContainer kmFloatLeft" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <table border="0" cellpadding="0" cellspacing="0" class="kmImageBlock" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody class="kmImageBlockOuter"> <tr> <td class="kmImageBlockInner" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding:9px;background-color:#CE0909;" valign="top"> <table align="left" border="0" cellpadding="0" cellspacing="0" class="kmImageContentContainer" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody> <tr> <td class="kmImageContent" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding:0;padding-top:0px;padding-bottom:0;padding-left:9px;padding-right:9px;text-align: center;"> <img align="center" alt="" class="kmImage" src="your company logo can go here" width="180" style="border:0;height:auto;line-height:100%;outline:none;text-decoration:none;padding-bottom:0;display:inline;vertical-align:bottom;max-width:180px;" /> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <table border="0" cellpadding="0" cellspacing="0" class="kmTextBlock" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody class="kmTextBlockOuter"> <tr> <td class="kmTextBlockInner" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;"> <table align="left" border="0" cellpadding="0" cellspacing="0" class="kmTextContentContainer" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody> <tr> <td class="kmTextContent" valign="top" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;padding-top:9px;padding-bottom:9px;padding-left:18px;padding-right:18px;'> <p style="margin:0;padding-bottom:1em">Dear Admin,</p> <p style="margin:0;padding-bottom:0">$headermessage$</p> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <table border="0" cellpadding="0" cellspacing="0" class="kmTableBlock kmTableMobile" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody class="kmTableBlockOuter"> <tr> <td class="kmTableBlockInner" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding-top:9px;padding-bottom:9px;padding-left:18px;padding-right:18px;"> <table align="left" border="0" cellpadding="0" cellspacing="0" class="kmTable" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;"> <thead> <tr> <th valign="top" class="kmTextContent tdwidth" style='color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;padding-top:4px;font-weight:bold;padding-right:0px;padding-left:0px;padding-bottom:4px;'> Fields </th> <th valign="top" class="kmTextContent" style='color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;padding-top:4px;font-weight:bold;padding-right:0px;padding-left:0px;padding-bottom:4px;'> Details </th> </tr> </thead> <tbody> <tr class="kmTableRow"> <td valign="top" class="kmTextContent tdwidth" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> Username </td> <td valign="top" class="kmTextContent" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;border-right:none;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">$username$</span> </td> </tr> <tr class="kmTableRow"> <td valign="top" class="kmTextContent" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> Email </td> <td valign="top" class="kmTextContent tdwidth" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;border-right:none;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">$email$</span> </td> </tr> <tr class="kmTableRow"> <td valign="top" class="kmTextContent tdwidth" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">Mobile No.</span> </td> <td valign="top" class="kmTextContent" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;border-right:none;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">$mobile$</span> </td> </tr> <tr class="kmTableRow"> <td valign="top" class="kmTextContent tdwidth" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">Age</span> </td> <td valign="top" class="kmTextContent" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;border-right:none;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">$age$</span> </td> </tr> <tr class="kmTableRow"> <td valign="top" class="kmTextContent tdwidth" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">Dob</span> </td> <td valign="top" class="kmTextContent" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;border-right:none;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">$dob$</span> </td> </tr> <tr class="kmTableRow"> <td valign="top" class="kmTextContent tdwidth" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">Address</span> </td> <td valign="top" class="kmTextContent" style='border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;color:#222;font-family:"Helvetica Neue", Arial;font-size:14px;line-height:130%;text-align:left;border-right:none;text-align:center;;border-top-style:solid;padding-bottom:4px;padding-right:0px;padding-left:0px;padding-top:4px;border-top-color:#d9d9d9;border-top-width:1px;'> <span style="color: #000000;">$address$</span> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> <tr> </tr> <tr> <td align="center" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <table border="0" cellpadding="0" cellspacing="0" class="templateRow" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody> <tr> <td class="rowContainer kmFloatLeft" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> </td> </tr> </tbody> </table> </td> </tr> </table> </td> </tr> </tbody> </table> <table border="0" cellpadding="0" cellspacing="0" class="templateContainer brandingContainer" width="600" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;border:0 none #aaa;background-color:transparent;border-radius:0"> <tbody> <tr> <td class="templateContainerInner" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding:0"> <table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tr> <td align="center" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <table border="0" cellpadding="0" cellspacing="0" class="templateRow" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody> <tr> <td class="rowContainer kmFloatLeft" valign="top" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <table border="0" cellpadding="0" cellspacing="0" class="kmImageBlock" width="100%" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0"> <tbody class="kmImageBlockOuter"> <tr> <td class="kmImageBlockInner" style="border-collapse:collapse;mso-table-lspace:0;mso-table-rspace:0;padding:9px;padding-top:25px;padding-bottom:25px;" valign="top"> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </td> </tr> </table> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </center> </body> </html>
user_profile.html
HTML代码的输出看起来像这样
第四步
最后,将以下代码粘贴到您的MainActivity中。
package com.emailsender; import androidx.appcompat.app.AppCompatActivity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMail(View view) { new MailCreator().execute(""); } public class MailCreator extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... strings) { try { MailSender sender = new MailSender(getBaseContext(), AppsConstants.SENDER_EMAIL,AppsConstants.SENDER_PASSWORD); sender.sendUserDetailWithImage("New User Added", "Hi", "Himanshu", AppsConstants.RECIPEINT_MAIL, "Himanshu Verma", "xyz@gmail.com", "+91 6075959010", "02/02/1994", "25","New Delhi, India",""); } catch (Exception e) { Log.e("SendMail", e.getMessage(), e); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); // Toast.makeText(getActivity(),"Mail Sent",Toast.LENGTH_LONG).show(); } } }
MainActivity.java
结果
Data received on Gmail
注意:如果一切都很好,但仍然没有收到电子邮件,请检查您的垃圾邮件文件夹,如果该电子邮件位于此处,请单击“标记为非垃圾邮件”。
结论
我创建了一个Android项目,以电子邮件形式发送用户数据。我已经使用JavaMail API发送电子邮件。在本文中,我发送了HTML表单和数据。
如果您对本文有任何疑问或疑问,请随时在评论部分询问我。另外,您可以在我的Github帐户上找到本文的完整源代码。
翻译自:https://towardsdatascience.com/send-email-with-beautiful-form-in-android-b12cd5cc9572
本文暂时没有评论,来添加一个吧(●'◡'●)