package org.apache.android.mail;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new org.apache.harmony.xnet.provider.jsse.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
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 sendMail(String subject, String body,
String sender, String recipients) throws Exception {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
分享到:
相关推荐
uMail.pas里面两个函数分别是调用OutLook2003和OutLook2007发送mail OutLook2003MailProc OutLook2007MailProc Outlook_TLB为Outlook接口单元,需要其他扩展功能,可以在这个单元中找找。
C# WinForm发送Mail,程式发送邮件
网站发送Mail功能类 .NET c# public static bool Send(string FromAddress, string FromAddressPWD, string FromDisplayName, string toAdd, string strTitle, string strBody, string SmtpAddress, Attachment ...
phpmailer,发送mail的php程序,没有进行mail发送的封装。 里边test.php加入了实例。
android后台发送mail的jar包
发送Mail用到的3个jar文件
java发送mail使用的jar (附带类子代码) 解压缩后,可以得到jar和类子代码 解压缩后,可以得到jar和类子代码 https://blog.csdn.net/sxzlc/article/details/104216877
用pb开发,发送MAIL成功率100%,自动发送MAIL系统
JSP提交Form表单后数据插入数据库并自动发送Mail
javax mail,带sun的jar包
主要给大家介绍了关于crontab执行结果未通过发送mail通知用户的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用linux系统具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
根据网上的smtp.php修改而来的,已经经过测试了,适合php5.5以上版本的
在本地下载个mail服务器软件(如CMailServer),设置好账户后,可以直接运行发送。
【Demo篇·Mail】
用java写的发送邮件的程序,是个可以不上服务器就可以发邮件的。
* $mail->setFrom("邮箱"); 设置发件人(可以不用) * $mail->setReceiver("收件人邮箱"); 设置收件人,多个收件人,调用多次 * $mail->setMailInfo("test", "<b>test</b>"); 设置邮件主题、内容 * $...
Delph下利用HTTPRIO控件发送WebMail例程!摘自李维的例子。
主要应用于邮件发送方面的两个方法。可以进行群发,也可以进行单独发送。由你喜好哦。想要的拿去吧!
java发送附件mail, 包阔多附件,文本mail,以及网页mail.