1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| public class MailClientRecv { private Session session; private Store store; private String username = "chenliangyu1980@126.com"; private String password = "1234567899"; private String popServer = "pop.126.com"; public void init()throws Exception { Properties props = new Properties(); props.put("mail.store.protocol", "pop3"); props.put("mail.imap.class", "com.sun.mail.imap.IMAPStore"); props.put("mail.pop3.class", "com.sun.mail.pop3.POP3Store");
session = Session.getInstance(props,null); session.setDebug(false);
store = session.getStore("pop3"); store.connect(popServer,username,password); } public void receiveMessage()throws Exception { String folderName = "inbox"; Folder folder=store.getFolder(folderName); if(folder==null) { throw new Exception(folderName+"邮件夹不存在"); } folder.open(Folder.READ_ONLY); System.out.println("您的收件箱有"+folder.getMessageCount()+"封邮件."); System.out.println("您的收件箱有"+folder.getUnreadMessageCount()+"封未读的邮件.");
Message[] messages=folder.getMessages(); for(int i=1;i<=3;i++) { System.out.println("------第"+i+"封邮件-------"); Message message = messages[i]; System.out.println((message.getFrom())[0]); System.out.println(message.getSubject()); System.out.println(); } folder.close(false); } public void close()throws Exception { store.close(); } public static void main(String[] args)throws Exception { MailClientRecv client=new MailClientRecv(); client.init(); client.receiveMessage(); client.close(); } }
public class MailClientSend { private Session session; private Transport transport; private String username = "lychen@sei.ecnu.edu.cn"; private String password = "1234567899"; private String smtpServer = "webmail.ecnu.edu.cn"; public void init()throws Exception { Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.class", "com.sun.mail.smtp.SMTPTransport"); props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.port", "25"); props.put("mail.smtp.auth", "true");
session = Session.getInstance(props,new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); session.setDebug(true); transport = session.getTransport(); } public void sendMessage()throws Exception{ Message msg = AttachmentMessage.generate(); transport.connect(); transport.sendMessage(msg, msg.getAllRecipients()); System.out.println("邮件已经成功发送"); } public void close()throws Exception { transport.close(); } public static void main(String[] args)throws Exception { MailClientSend client=new MailClientSend(); client.init(); client.sendMessage(); client.close(); } }
String from = "lychen@sei.ecnu.edu.cn "; String to = "chenliangyu1980@126.com"; String subject = "test"; String body = "您好,这是来自一封chenliangyu的测试邮件";
Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSentDate(new Date());
message.setSubject(subject);
message.setText(body);
message.saveChanges(); return message;
String from = "lychen@sei.ecnu.edu.cn "; String to = "chenliangyu1980@126.com"; String subject = "HTML邮件"; String body = "<a href=http://www.ecnu.edu.cn>" + "<h4>欢迎大家访问我们的网站</h4></a></br>" + "<img src=\"https://news.ecnu.edu.cn/_upload/article/images/2e/e2/6b554d034c9192101208c732195e/16a6ec66-6729-4469-a5f4-0435e0f2e66a.jpg\">";
Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSentDate(new Date());
message.setSubject(subject);
message.setContent(body, "text/html;charset=gb2312");
message.saveChanges(); return message;
String from = "lychen@sei.ecnu.edu.cn "; String to = "chenliangyu1980@126.com"; String subject = "多附件邮件"; String body = "<a href=http://www.ecnu.edu.cn>" + "欢迎大家访问我们的网站</a></br>";
Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject);
MimeBodyPart contentPart = createContent(body); MimeBodyPart attachPart1 = createAttachment("c:/temp/ecnu4.jpg"); MimeBodyPart attachPart2 = createAttachment("c:/temp/ecnu5.jpg");
MimeMultipart allMultipart = new MimeMultipart("mixed"); allMultipart.addBodyPart(contentPart); allMultipart.addBodyPart(attachPart1); allMultipart.addBodyPart(attachPart2);
message.setContent(allMultipart); message.saveChanges(); return message;
public static MimeBodyPart createContent(String body) throws Exception { MimeBodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(body,"text/html;charset=gb2312"); return htmlBodyPart; }
public static MimeBodyPart createAttachment(String filename) throws Exception { MimeBodyPart attachPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource(filename); attachPart.setDataHandler(new DataHandler(fds)); attachPart.setFileName(fds.getName()); return attachPart; }
|