「java发邮件jar」java发邮件附件名乱码
今天给各位分享java发邮件jar的知识,其中也会对java发邮件附件名乱码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java发邮件需要什么jar包
使用JavaMail发送邮件需要用到mail.jar和activtion.jar两个包。 该类实现了较完整的邮件发送功能,包括以HTML格式发送,添加附件和抄送人。下面是具体的代码
怎样用java发送邮件
首先下载 JavaMail jar 包,并导入到项目中。下载地址
编写代码,代码如下:
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class App45 {
public static void main(String[] args) throws AddressException, MessagingException {
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "邮件发送服务器");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 设置发件人邮件帐号和密码
return new PasswordAuthentication("邮件帐号", "密码");
}
});
MimeMessage message = new MimeMessage(session);
// 设置发件人邮件地址
message.setFrom(new InternetAddress("发件人邮件地址"));
// 设置收件人邮件地址
message.addRecipient(Message.RecipientType.TO, new InternetAddress("收件人邮件地址"));
message.setSubject("这里是邮件主题。");
message.setText("这里是邮件内容。");
Transport.send(message);
}
}
怎么用java发送邮件,像园子那样
1.首先你需要一个邮箱中转发送站(听着很高端的样子),说白了就是注册一个邮箱作为你的发送邮件平台,然后通过编程调用平台发送邮件(也就是你注册某个邮箱,然后开通SMTP/POP3协议,在编程中,拿着你的KEY去发送邮件),我试过很多种邮箱,QQ貌似不能用,网易经常报错,建议用新浪的,我用基本没出过问题。
2.去网上下载java开源的发送邮件工具类:mail.jar,并导入myeclipse/eclipse的引用。
3.编程(工具类)
java实现发送邮件功能
要实现邮件发送功能需要导入包:mail.jar
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.demo.action;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.demo.form.DemoForm;
public class DemoAction extends Action {
private static final String CONTENT_TYPE = "test/html;charset=GB2312";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DemoForm demoForm = (DemoForm) form;
System.out.println("标题是" + demoForm.getBiaoti());
System.out.println("内容是" + demoForm.getNeirong());
try {
response.setContentType(CONTENT_TYPE);
String smtphost = "smtp.nj.headware.cn"; // 发送邮件服务器
String user = "q0000015369"; // 邮件服务器登录用户名
String password = "Queshuwen26"; // 邮件服务器登录密码
String from = "q0000015369@nj.headware.cn"; //
String to = "c0000016205@nj.headware.cn"; // 收件人邮件地址
String subject = demoForm.getBiaoti(); // 邮件标题
String body = demoForm.getNeirong(); // 邮件内容
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth", "true");
Session ssn = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(ssn);
InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, user, password);
transport.sendMessage(message, message
.getRecipients(Message.RecipientType.TO));
// transport.send(message);
transport.close();
return mapping.findForward("succ");
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("fail");
}
}
}
关于java发邮件jar和java发邮件附件名乱码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-29,除非注明,否则均为
原创文章,转载请注明出处。