<legend id='i0NsP'><style id='i0NsP'><dir id='i0NsP'><q id='i0NsP'></q></dir></style></legend>
    <i id='i0NsP'><tr id='i0NsP'><dt id='i0NsP'><q id='i0NsP'><span id='i0NsP'><b id='i0NsP'><form id='i0NsP'><ins id='i0NsP'></ins><ul id='i0NsP'></ul><sub id='i0NsP'></sub></form><legend id='i0NsP'></legend><bdo id='i0NsP'><pre id='i0NsP'><center id='i0NsP'></center></pre></bdo></b><th id='i0NsP'></th></span></q></dt></tr></i><div id='i0NsP'><tfoot id='i0NsP'></tfoot><dl id='i0NsP'><fieldset id='i0NsP'></fieldset></dl></div>

      <tfoot id='i0NsP'></tfoot>

      1. <small id='i0NsP'></small><noframes id='i0NsP'>

        • <bdo id='i0NsP'></bdo><ul id='i0NsP'></ul>
      2. 使用 SMTP 在没有意图的情况下在 android 中发送邮件

        时间:2023-06-11
        <legend id='jvrgd'><style id='jvrgd'><dir id='jvrgd'><q id='jvrgd'></q></dir></style></legend>
            <tbody id='jvrgd'></tbody>

            <bdo id='jvrgd'></bdo><ul id='jvrgd'></ul>
            <i id='jvrgd'><tr id='jvrgd'><dt id='jvrgd'><q id='jvrgd'><span id='jvrgd'><b id='jvrgd'><form id='jvrgd'><ins id='jvrgd'></ins><ul id='jvrgd'></ul><sub id='jvrgd'></sub></form><legend id='jvrgd'></legend><bdo id='jvrgd'><pre id='jvrgd'><center id='jvrgd'></center></pre></bdo></b><th id='jvrgd'></th></span></q></dt></tr></i><div id='jvrgd'><tfoot id='jvrgd'></tfoot><dl id='jvrgd'><fieldset id='jvrgd'></fieldset></dl></div>

            <small id='jvrgd'></small><noframes id='jvrgd'>

              • <tfoot id='jvrgd'></tfoot>

                  本文介绍了使用 SMTP 在没有意图的情况下在 android 中发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我正在开发一个 Android 应用程序,该应用程序将通过单击按钮发送邮件.代码起初可以工作,但由于某种原因它现在不能工作.谁能帮我解决这个问题?xyz@outlook.com 是收件人.abc@gmail.com 是发件人.我已经对邮件的主题和正文进行了硬编码.

                  Hi I am developing an android app which will send mail on click of a button. Code worked at first but due to some reason its not working now. Could anyone please help me with this? xyz@outlook.com is the recipient. abc@gmail.com is the sender. I have hard coded the subject and the body of the mail.

                  package com.example.clc_construction;
                  import java.io.File;
                  import java.io.UnsupportedEncodingException;
                  import java.util.Properties;
                  import javax.activation.DataHandler;
                  import javax.activation.DataSource;
                  import javax.activation.FileDataSource;
                  import javax.mail.Message;
                  import javax.mail.MessagingException;
                  import javax.mail.Multipart;
                  import javax.mail.PasswordAuthentication;
                  import javax.mail.Session;
                  import javax.mail.Transport;
                  import javax.mail.internet.AddressException;
                  import javax.mail.internet.InternetAddress;
                  import javax.mail.internet.MimeBodyPart;
                  import javax.mail.internet.MimeMessage;
                  import javax.mail.internet.MimeMultipart;
                  import android.app.Activity;
                  import android.app.ProgressDialog;  
                  import android.content.Intent;
                  import android.graphics.Bitmap;
                  import android.os.AsyncTask;
                  import android.os.Bundle;
                  import android.os.Environment;
                  
                  
                  public class Email extends Activity
                  {
                  public String jobNo;
                  public String teamNo;
                  private static final String username = "abc@gmail.com";
                  private static final String password = "000000";
                  private static final String emailid = "xyz@outlook.com";
                  private static final String subject = "Photo";
                  private static final String message = "Hello";
                  private Multipart multipart = new MimeMultipart();
                  private MimeBodyPart messageBodyPart = new MimeBodyPart();
                  public File mediaFile;
                  
                  @Override
                  protected void onCreate(Bundle savedInstanceState)
                  {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.camera_screen);
                      Intent intent = getIntent();
                      jobNo = intent.getStringExtra("Job_No");
                      teamNo = intent.getStringExtra("Team_No"); 
                      sendMail(emailid,subject,message);
                  
                  }
                  private void sendMail(String email, String subject, String messageBody)
                   {
                          Session session = createSessionObject();
                  
                          try {
                              Message message = createMessage(email, subject, messageBody, session);
                              new SendMailTask().execute(message);
                          }
                          catch (AddressException e)
                          {
                              e.printStackTrace();
                          }
                          catch (MessagingException e)
                          {
                              e.printStackTrace();
                          }
                          catch (UnsupportedEncodingException e)
                          {
                              e.printStackTrace();
                          }
                      }
                  
                  
                  private Session createSessionObject()
                  {
                      Properties properties = new Properties();
                      properties.put("mail.smtp.auth", "true");
                      properties.put("mail.smtp.starttls.enable", "true");
                      properties.put("mail.smtp.host", "smtp.gmail.com");
                      properties.put("mail.smtp.port", "587");
                  
                      return Session.getInstance(properties, new javax.mail.Authenticator()
                      {
                          protected PasswordAuthentication getPasswordAuthentication()
                          {
                              return new PasswordAuthentication(username, password);
                          }
                      });
                  }
                  
                  private Message createMessage(String email, String subject, String messageBody, Session session) throws 
                  
                  MessagingException, UnsupportedEncodingException
                  {
                      Message message = new MimeMessage(session);
                      message.setFrom(new InternetAddress("xzy@outlook.com", "Naveed Qureshi"));
                      message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
                      message.setSubject(subject);
                      message.setText(messageBody);
                      return message;
                  }
                  
                  
                  
                  public class SendMailTask extends AsyncTask<Message, Void, Void>
                  {
                      private ProgressDialog progressDialog;
                  
                      @Override
                      protected void onPreExecute()
                      {
                          super.onPreExecute();
                          progressDialog = ProgressDialog.show(Email.this, "Please wait", "Sending mail", true, false);
                      }
                  
                      @Override
                      protected void onPostExecute(Void aVoid)
                      {
                          super.onPostExecute(aVoid);
                          progressDialog.dismiss();
                      }
                  
                      protected Void doInBackground(javax.mail.Message... messages)
                      {
                          try
                          {
                              Transport.send(messages[0]);
                          } catch (MessagingException e)
                          {
                              e.printStackTrace();
                          }
                          return null;
                      }
                  }
                  }
                  

                  推荐答案

                  放入你的清单文件,

                  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
                  

                  检查您是否有互联网连接,

                  check if you have internet connection,

                  public boolean isOnline() {
                      ConnectivityManager cm =
                          (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                      NetworkInfo netInfo = cm.getActiveNetworkInfo();
                      if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                          return true;
                      }
                      return false;
                  }
                  

                  并最终使用此代码发送电子邮件

                  and finnaly use this code to send email

                  final String username = "username@gmail.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.gmail.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("from-email@gmail.com"));
                          message.setRecipients(Message.RecipientType.TO,
                              InternetAddress.parse("to-email@gmail.com"));
                          message.setSubject("Testing Subject");
                          message.setText("Dear Mail Crawler,"
                              + "
                  
                   No spam to my email, please!");
                  
                          MimeBodyPart messageBodyPart = new MimeBodyPart();
                  
                          Multipart multipart = new MimeMultipart();
                  
                          messageBodyPart = new MimeBodyPart();
                          String file = "path of file to be attached";
                          String fileName = "attachmentName"
                          DataSource source = new FileDataSource(file);
                          messageBodyPart.setDataHandler(new DataHandler(source));
                          messageBodyPart.setFileName(fileName);
                          multipart.addBodyPart(messageBodyPart);
                  
                          message.setContent(multipart);
                  
                          Transport.send(message);
                  
                          System.out.println("Done");
                  
                      } catch (MessagingException e) {
                          throw new RuntimeException(e);
                      }
                  

                  这篇关于使用 SMTP 在没有意图的情况下在 android 中发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:符号(方法和函数)的基地址(程序计数器)不匹配.关闭 1 下一篇:使用带有 smtp 但没有 SSL 的 JavaMail API 在 android 中发送电子邮件

                  相关文章

                    <i id='NleGs'><tr id='NleGs'><dt id='NleGs'><q id='NleGs'><span id='NleGs'><b id='NleGs'><form id='NleGs'><ins id='NleGs'></ins><ul id='NleGs'></ul><sub id='NleGs'></sub></form><legend id='NleGs'></legend><bdo id='NleGs'><pre id='NleGs'><center id='NleGs'></center></pre></bdo></b><th id='NleGs'></th></span></q></dt></tr></i><div id='NleGs'><tfoot id='NleGs'></tfoot><dl id='NleGs'><fieldset id='NleGs'></fieldset></dl></div>
                      <bdo id='NleGs'></bdo><ul id='NleGs'></ul>
                    1. <tfoot id='NleGs'></tfoot>
                    2. <legend id='NleGs'><style id='NleGs'><dir id='NleGs'><q id='NleGs'></q></dir></style></legend>

                      <small id='NleGs'></small><noframes id='NleGs'>