C#完成HTTP下載文件的辦法。本站提示廣大學習愛好者:(C#完成HTTP下載文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成HTTP下載文件的辦法正文
全體後果:
發送端:網易郵箱;吸收端:qq郵箱。
1.web前端
2.在網易郵箱“已發送”中可以看見經由過程java代碼發送的郵件
3.異樣在qq郵箱中也能夠看到如許的後果
完成進程:
1.web前端(bootstrap結構)
<form action="mailAction!sendMail" method="post" name="mailForm" id="mailFormId"> <ul class="list-group"> <li class="list-group-item"> <div class="input-group"> <span class="input-group-addon" id="basic-addon1">姓名:</span> <input type="text" class="form-control" placeholder="your name" name="mailForm.name" aria-describedby="basic-addon1"> </div> </li> <li class="list-group-item"> <div class="input-group"> <span class="input-group-addon" id="basic-addon2">德律風:</span> <input type="text" class="form-control" placeholder="your phone" name="mailForm.phone" aria-describedby="basic-addon1"> </div> </li> <li class="list-group-item"> <div class="input-group"> <span class="input-group-addon" id="basic-addon2">郵件:</span> <input type="text" class="form-control" placeholder="your e-mail" name="mailForm.e_mail" aria-describedby="basic-addon1"> </div> </li> <li class="list-group-item" > <span class="label label-default blog-label-1">新聞:</span> <br><br> <textarea rows="10" name="mailForm.content" placeholder="請輸出新聞(不要跨越500個字符)"></textarea> </li> <li class="list-group-item"> <center><button onclick="$('#mailFormId').submit();" type="button" class="btn btn-success">發送郵件</button></center> </li> </ul> </form>
2.起首預備一個XML的模板(<xml-body>包括的是郵件的html格局的文本)。
<?xml version="1.0" encoding="UTF-8"?> <!-- 將空格換玉成角的空格,便可以包管html不會將空格過濾失落 --> <xml-body> <html> <head> </head> <body > <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr> <td > <table align="center" border="0" cellpadding="0" cellspacing="0" width="600" > <tbody><tr> <td align="center" bgcolor="#70bbd9" > <img src="cid:{4}" alt="Creating Email Magic" width="300" height="230" /> </td> </tr> <tr> <td bgcolor="#ffffff" > <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr> <td > <b> 發件人: {0} <br/> 德律風: {1} <br/> 郵箱: {2} <br/> 內容:<br/> </b> </td> </tr> <tr> <td > {3} </td> </tr> <tr> <td> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr> <td width="260" valign="top"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr> <td> <img src="cid:{5}" alt="" width="100%" height="140" /> </td> </tr> </tbody></table> </td> <td width="20"> </td> <td width="260" valign="top"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr> <td> <img src="cid:{6}" alt="" width="100%" height="140" /> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> <tr> <td bgcolor="#ee4c50" > <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr> <td align="right" width="25%"> <table border="0" cellpadding="0" cellspacing="0"> <tbody><tr> <td > <img src="cid:{7}" alt="Twitter" width="38" height="38" border="0"/> </td> <td > <img src="cid:{8}" alt="Facebook" width="38" height="38" border="0"/> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </body> </html> </xml-body>
個中模板中會有一些標識符,如{i},表照樣字符串的預留地位,然後經由過程MessageFormat格局化如許的新聞,然後將格局化後的字符串拔出到形式中的恰當地位。
//獲得XML的模板 String XML_path = ServletActionContext.getServletContext().getRealPath("/mailTemplate")+"/myMailTemplete.xml"; String str=new ReaderXML().read(XML_path); Object[] obj=new Object[]{mailForm.getName(), mailForm.getPhone(), mailForm.getE_mail(), mailForm.getContent(), "e_mail", "left", "right", "tw", "fb"}; //MessageFormat可以格局化如許的新聞,然後將格局化後的字符串拔出到形式中的恰當地位 String tcontent = MessageFormat.format(str, obj);
終究XML模板中的{i}分離被obj[i]調換了。
3.寫一個對XML模板的類ReaderXML
public class ReaderXML { public String read(String path){ String str=null; str=reader(path); return str; } private String reader(String path){ SAXReader reader=new SAXReader(); String str=null; try { Document d=reader.read(new File(path)); Element e=d.getRootElement(); Element htmle=e.element("html"); str=htmle.asXML(); } catch (DocumentException e) { e.printStackTrace(); } return str; } }
4.最初就是我們的掌握器類MailAction
在 HTML 格局的注釋中內含圖片是應用MimeBodyPart類的setContentID() 辦法設置對應的資本文件的獨一標識符,即 MIME 協定關於郵件的構造組織格局中的 Content-ID 頭字段,對應著XML模板中的cid:{i}標識, 如<img src="cid:{8}"/>(注:{i}會經由過程MessageFormat.format調換成對應的字符串)
public class MailAction extends ActionSupport{ private MailForm mailForm; public MailForm getMailForm() { return mailForm; } public void setMailForm(MailForm mailForm) { this.mailForm = mailForm; } //添加內嵌圖片 private MimeBodyPart createImageMimeBodyPart(String imageName) throws MessagingException, UnsupportedEncodingException{ FileDataSource fds=new FileDataSource(ServletActionContext.getServletContext().getRealPath("/image")+"/" + imageName + ".gif"); MimeBodyPart mbp=new MimeBodyPart(); DataHandler dh=new DataHandler(fds); mbp.setDataHandler(dh); //設置對應的資本文件的獨一標識符,即 MIME 協定關於郵件的構造組織格局中的 Content-ID 頭字段; mbp.setHeader("Content-ID", imageName); mbp.setFileName(MimeUtility.encodeText(fds.getName())); return mbp; } public String sendMail(){ try { HttpServletRequest request = ServletActionContext.getRequest(); String pwd = "************";//發件人郵箱暗碼 String mailfrom = "***********@163.com"; //網易的郵箱 String wangyiFrom = mailfrom.substring(0, mailfrom.indexOf('@'));//網易郵箱的用戶名 String tu = "163.com"; //發件人郵箱的後綴域名 String tto= "**********@qq.com"; //吸收郵件的郵箱 String ttitle= "有人接洽你---來自胡峻峥的小我網站"; //依據其物理途徑,獲得XML的模板 String XML_path = ServletActionContext.getServletContext().getRealPath("/mailTemplate")+"/myMailTemplete.xml"; String str=new ReaderXML().read(XML_path); Object[] obj=new Object[]{mailForm.getName(), mailForm.getPhone(), mailForm.getE_mail(), mailForm.getContent(), "e_mail", "left", "right", "tw", "fb"}; //MessageFormat可以格局化如許的新聞,然後將格局化後的字符串拔出到形式中的恰當地位 String tcontent = MessageFormat.format(str, obj); Properties props=new Properties(); props.put("mail.smtp.host","smtp."+tu);//郵箱SMTP辦事器地址端口 props.put("mail.smtp.auth","true");//如許能力經由過程驗證 Session s=Session.getInstance(props); s.setDebug(true); MimeMessage message=new MimeMessage(s); //給新聞對象設置發件人/收件人/主題/發信時光 InternetAddress from; from = new InternetAddress(mailfrom);//發件人的qq郵箱 message.setFrom(from); InternetAddress to=new InternetAddress(tto);//收件人的郵箱 message.setRecipient(Message.RecipientType.TO,to); message.setSubject(ttitle); message.setSentDate(new Date()); //給新聞對象設置內容 BodyPart mbp=new MimeBodyPart();//新建一個寄存函件內容的BodyPart對象 mbp.setContent(tcontent,"text/html;charset=gb2312");//給BodyPart對象設置內容和格局/編碼方法 // 用於組合文本和圖片,"related"型的MimeMultipart對象 Multipart mm=new MimeMultipart("related");//新建一個MimeMultipart對象用來寄存BodyPart對象(現實上可以寄存多個) mm.addBodyPart(mbp);//將BodyPart參加到MimeMultipart對象中(可以參加多個BodyPart) //添加圖片 mm.addBodyPart(createImageMimeBodyPart("e_mail")); mm.addBodyPart(createImageMimeBodyPart("left")); mm.addBodyPart(createImageMimeBodyPart("right")); mm.addBodyPart(createImageMimeBodyPart("tw")); mm.addBodyPart(createImageMimeBodyPart("fb")); message.setContent(mm);//把mm作為新聞對象的內容 message.saveChanges(); Transport transport=s.getTransport("smtp"); transport.connect("smtp."+tu, wangyiFrom, pwd); //這裡的wangyiFrom為發件人網易賬號 transport.sendMessage(message,message.getAllRecipients()); transport.close(); ActionContext.getContext().getSession().put("operations", "郵件發送勝利, 請耐煩期待答復!"); } catch (Exception e) { System.out.println(e.toString()); ActionContext.getContext().getSession().put("errors", e.toString()); return "errors"; } return "sendMail"; } }
以上就是本文的全體內容,願望對年夜家的進修有所贊助。