⑴ java 发送邮件,内容是要从数据库中读取的数据并列成表格的状态发送出去
其他省略,直接上邮件内容,以html格式编写:
String[] toMail = {"发送邮箱1","发送邮箱[email protected]"};
String subject = "Mark-最近一周的生产记录异常数据!!!";
//一下即为邮件内容content
String html = "<html><body><font size='15' color='black'><table align='left' border='1' cellpadding='1' cellspacing='0'>"
+ "<tr style='font-size: 16px;' height='20px' bgcolor='#DCDCDC'>"
+ "<th>BadgeNo</th><th>Shift</th><th>Section</th><th>SerialNo</th><th>DateTimeIn</th><th>TimeSpan/秒</th></tr>";
for (EMS_Outliers Outliers : list) {
html += "<tr><td>"+Outliers.getBadgeNo()+"</td><td align='center'>"+Outliers.getShift()+"</td><td>"+Outliers.getSection()+"</td><td>"
+Outliers.getSerialNo()+"</td><td>"+Outliers.getDateTimeIn()+"</td><td align='center'>"+Outliers.getTimeSpan()+"</td></tr>";
}
html += "</table></font></body></html>";
try {//一下为调用的发送邮件的方法
MailGroupSending.sendhtmlmail(mailUser, mailpwd, toMail, subject, html, null, null);
} catch (Exception e) {
e.printStackTrace();
}
⑵ JAVA怎么获取邮件内容
给你一个参考,很详细,你只需要对照看下获取邮件内容专的那部分就可以了属。
http://blog.csdn.net/xyang81/article/details/7675160
⑶ java如何使用ssl连接qq邮箱
Gmail目前已经启用了POP3和SMTP服务,与其他邮箱不同的是Gmail提供的POP3和SMTP是使用安全套接字层SSL的,因此常规的JavaMail程序是无法收发邮件的,下面是使用JavaMail如何收取Gmail邮件以及发送邮件的代码:
1.[代码]GmailFetch.java跳至[1][2][全屏预览]
01packagelius.javamail.ssl;
02
03importjava.io.UnsupportedEncodingException;
04importjava.security.*;
05importjava.util.Properties;
06importjavax.mail.*;
07importjavax.mail.internet.InternetAddress;
08importjavax.mail.internet.MimeUtility;
09
10/**
11*用于收取Gmail邮件
12*@authorWinterLau
13*/
14publicclassGmailFetch{
15
16publicstaticvoidmain(Stringargv[])throwsException{
17
18Security.addProvider(newcom.sun.net.ssl.internal.ssl.Provider());
19finalStringSSL_FACTORY="javax.net.ssl.SSLSocketFactory";
20
21//GetaPropertiesobject
22Propertiesprops=System.getProperties();
23props.setProperty("mail.pop3.socketFactory.class",SSL_FACTORY);
24props.setProperty("mail.pop3.socketFactory.fallback","false");
25props.setProperty("mail.pop3.port","995");
26props.setProperty("mail.pop3.socketFactory.port","995");
27
28//以下步骤跟一般的JavaMail操作相同
29Sessionsession=Session.getDefaultInstance(props,null);
30
31//请将红色部分对应替换成你的邮箱帐号和密码
32URLNameurln=newURLName("pop3","pop.gmail.com",995,null,
33"[邮箱帐号]","[邮箱密码]");
34Storestore=session.getStore(urln);
35Folderinbox=null;
36try{
37store.connect();
38inbox=store.getFolder("INBOX");
39inbox.open(Folder.READ_ONLY);
40FetchProfileprofile=newFetchProfile();
41profile.add(FetchProfile.Item.ENVELOPE);
42Message[]messages=inbox.getMessages();
43inbox.fetch(messages,profile);
44System.out.println("收件箱的邮件数:"+messages.length);
45for(inti=0;i<messages.length;i++){
46//邮件发送者
47Stringfrom=decodeText(messages[i].getFrom()[0].toString());
48InternetAddressia=newInternetAddress(from);
49System.out.println("FROM:"+ia.getPersonal()+'('+ia.getAddress()+')');
50//邮件标题
51System.out.println("TITLE:"+messages[i].getSubject());
52//邮件大小
53System.out.println("SIZE:"+messages[i].getSize());
54//邮件发送时间
55System.out.println("DATE:"+messages[i].getSentDate());
56}
57}finally{
58try{
59inbox.close(false);
60}catch(Exceptione){}
61try{
62store.close();
63}catch(Exceptione){}
64}
65}
66
(Stringtext)
{
69if(text==null)
70returnnull;
71if(text.startsWith("=?GB")||text.startsWith("=?gb"))
72text=MimeUtility.decodeText(text);
73else
74text=newString(text.getBytes("ISO8859_1"));
75returntext;
76}
77
78}
2.[代码]GmailSender.java
01packagelius.javamail.ssl;
02
03importjava.security.Security;
04importjava.util.Date;
05importjava.util.Properties;
06
07importjavax.mail.Authenticator;
08importjavax.mail.Message;
09importjavax.mail.MessagingException;
10importjavax.mail.PasswordAuthentication;
11importjavax.mail.Session;
12importjavax.mail.Transport;
13importjavax.mail.internet.AddressException;
14importjavax.mail.internet.InternetAddress;
15importjavax.mail.internet.MimeMessage;
16
17/**
18*使用Gmail发送邮件
19*@authorWinterLau
20*/
21publicclassGmailSender{
22
23publicstaticvoidmain(String[]args)throwsAddressException,MessagingException{
24Security.addProvider(newcom.sun.net.ssl.internal.ssl.Provider());
25finalStringSSL_FACTORY="javax.net.ssl.SSLSocketFactory";
26//GetaPropertiesobject
27Propertiesprops=System.getProperties();
28props.setProperty("mail.smtp.host","smtp.gmail.com");
29props.setProperty("mail.smtp.socketFactory.class",SSL_FACTORY);
30props.setProperty("mail.smtp.socketFactory.fallback","false");
31props.setProperty("mail.smtp.port","465");
32props.setProperty("mail.smtp.socketFactory.port","465");
33props.put("mail.smtp.auth","true");
34finalStringusername="[邮箱帐号]";
35finalStringpassword="[邮箱密码]";
36Sessionsession=Session.getDefaultInstance(props,newAuthenticator(){
(){
(username,password);
39}});
40
41//--Createanewmessage--
42Messagemsg=newMimeMessage(session);
43
44//--SettheFROMandTOfields--
45msg.setFrom(newInternetAddress(username+"@mo168.com"));
46msg.setRecipients(Message.RecipientType.TO,
47InternetAddress.parse("[收件人地址]",false));
48msg.setSubject("Hello");
49msg.setText("Howareyou");
50msg.setSentDate(newDate());
51Transport.send(msg);
52
53System.out.println("Messagesent.");
54}
55}
⑷ java中如何从文件中读取数据
分为读字节,读字符两种读法
◎◎◎FileInputStream 字节输入流读文件◎◎◎
public class Maintest {
public static void main(String[] args) throws IOException {
File f=new File("G:\\just for fun\\xiangwei.txt");
FileInputStream fin=new FileInputStream(f);
byte[] bs=new byte[1024];
int count=0;
while((count=fin.read(bs))>0)
{
String str=new String(bs,0,count); //反复定义新变量:每一次都 重新定义新变量,接收新读取的数据
System.out.println(str); //反复输出新变量:每一次都 输出重新定义的新变量
}
fin.close();
}
}
◎◎◎FileReader 字符输入流读文件◎◎◎
public class Maintest {
public static void main(String[] args) throws IOException {
File f=new File("H:\\just for fun\\xiangwei.txt");
FileReader fre=new FileReader(f);
BufferedReader bre=new BufferedReader(fre);
String str="";
while((str=bre.readLine())!=null) //●判断最后一行不存在,为空
{
System.out.println(str);
}
bre.close();
fre.close();
}
}
⑸ 如何用JAVA来读取一个邮箱并取出用户名
String.splite("@")会返回一个数组,数组第一个元素就是邮件用户名