gpt4 book ai didi

android - 使用SMTP在没有 Intent 的情况下在android中发送邮件

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:05 25 4
gpt4 key购买 nike

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

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;
}
}
}

最佳答案

放入你的 list 文件,

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

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

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

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

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,"
+ "\n\n 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);
}

关于android - 使用SMTP在没有 Intent 的情况下在android中发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25136025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com