⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 send.java

📁 这个是一个基于javamail 的邮件接受和发送部分 虽GUI没有 但是功能的东西已经完毕 只为借鉴
💻 JAVA
字号:
package servlet;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class send extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public send() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br> this is use to send email  this  method  need user name password
	 * mail from mail to mail message file
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		Properties props = new Properties();
		Session sendMailSession;
		Store store;
		Transport transport;
		try {
			props.put("mail.smtp.host", "smtp.163.com");
			sendMailSession = Session.getDefaultInstance(props, new Email_ca("用户名","密码"));// 验证 
			Message newMessage = new MimeMessage(sendMailSession);
			newMessage
					.setFrom(new InternetAddress(request.getParameter("from")));
			newMessage.setRecipient(Message.RecipientType.TO,
					new InternetAddress(request.getParameter("to")));
			newMessage.setSubject(request.getParameter("subject"));
			newMessage.setSentDate(new Date());
			newMessage.setText(request.getParameter("text"));
			Multipart mp = new MimeMultipart();
			MimeBodyPart mbp = new MimeBodyPart();
			FileDataSource fds = new FileDataSource("路经");
			// 得到附件本身并至入BodyPart
			mbp.setDataHandler(new DataHandler(fds));
			// 得到文件名同样至入BodyPart
			mbp.setFileName(fds.getName());
			mp.addBodyPart(mbp);
			newMessage.setContent(mp);

			transport = sendMailSession.getTransport("smtp");
			transport.send(newMessage);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public class Email_ca extends javax.mail.Authenticator {
		private String user = null;
		private String pwd = null;

		public Email_ca(String user, String pwd) {
			super();
			this.user = user;
			this.pwd = pwd;
		}

		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(user, pwd);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -