Android 发送邮箱

Android邮箱可用于app将错误的消息发送到开发者的邮箱

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailManager {
    private static String mailServerHos="smtp.126.com";    
    private static String mailServerPort = "25";   

    // 邮件发送者的地址    
    private static  String fromAddress="shui501@126.com";    
    // 邮件接收者的地址    
    private static String toAddress="632095376@qq.com";    
    // 登陆邮件发送服务器的用户名和密码    
    private static String userName="shui501@126.com";    
    private static String password="*******";    
    // 是否需要身份验证    
    private static boolean validate = true;    
    // 邮件主题    
    private static String subject="主题";    
    public static boolean sendmail(String errorMsg) {
         // 根据邮件会话属性和密码验证器构造一个发送邮件的session  
         Properties props = new Properties();
         props.put("mail.smtp.host", mailServerHos);
         props.put("mail.smtp.port", mailServerPort);
         props.put("mail.smtp.auth", "true");
         props.put("mail.user", userName);
         props.put("mail.password", password);
         Authenticator authenticator = new Authenticator() {
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
                 // 用户名、密码
                 return new PasswordAuthentication(userName, password);
             }
         };
         Session sendMailSession = Session.getDefaultInstance(props,authenticator);    
         try 
         {    
             // 根据session创建一个邮件消息    
             Message mailMessage = new MimeMessage(sendMailSession);    
             // 创建邮件发送者地址    
             Address from = new InternetAddress(fromAddress);    
             // 设置邮件消息的发送者    
             mailMessage.setFrom(from);    
             // 创建邮件的接收者地址,并设置到邮件消息中    
             Address to = new InternetAddress(toAddress);    
             mailMessage.setRecipient(Message.RecipientType.TO,to);    
             // 设置邮件消息的主题    
             mailMessage.setSubject(subject);    
             // 设置邮件消息发送的时间    
             mailMessage.setSentDate(new Date());    
             // 设置邮件消息的主要内容    
             String mailContent = errorMsg;    
             mailMessage.setText(mailContent);    
             // 发送邮件    
             Transport.send(mailMessage);   
             return true;    
         } 
         catch (MessagingException ex) 
         {    
             ex.printStackTrace();    
         }    
         return false;    
    }
}
以下是一个发送邮件的基本代码示例,你需要在代码中填写你的邮箱地址和密码,以及收件人、主题和内容等信息: ```java public void sendEmail() { String email = "yourEmail@gmail.com"; String password = "yourPassword"; String recipient = "recipientEmail@gmail.com"; String subject = "Test Email"; String message = "This is a test email from Android."; String[] recipients = {recipient}; Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("message/rfc822"); intent.putExtra(Intent.EXTRA_EMAIL, recipients); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, message); try { // Authenticate with your Gmail account Authenticator auth = new Authenticator() { // Override the getPasswordAuthentication method protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(email, password); } }; // Create a new session with Gmail SMTP server Session session = Session.getInstance(getProperties(), auth); // Send email using the created session Transport.send(new Message(session, recipients, subject, message)); Toast.makeText(this, "Email sent successfully", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "Failed to send email", Toast.LENGTH_SHORT).show(); } } private Properties getProperties() { 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"); return props; } ``` 注意:在使用此代码之前,请确保已经在你的 Android 项目中添加了邮件发送相关的依赖库。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值