Android发送邮件

/* 发送邮件的地址 */
			Uri uri = Uri.parse("mailto:fengsheng.studio@hotmail.com");  
			
			/* 创建Intent */
			Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
			
			/* 设置邮件的主题 */
			it .putExtra(android.content.Intent.EXTRA_SUBJECT, "数据备份");
			
			/* 设置邮件的内容 */
			it .putExtra(android.content.Intent.EXTRA_TEXT, "本次计数:"+miCount);
			
			/* 开启 */
			startActivity(it);

 

以下是一个发送邮件的基本代码示例,你需要在代码中填写你的邮箱地址和密码,以及收件人、主题和内容等信息: ```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 项目中添加了邮件发送相关的依赖库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值