Use jmail to receive emails

Source: Internet
Author: User
Tags dbase
  1. Jmail. Message MSG = new jmail. Message ();
  2. Jmail. POP3 JPOP = new jmail. POP3 ();
  3. Jmail. attachments ATTS;
  4. Jmail. Attachment ATT;
  5. // Username is the user name. This method obtains the pop settings of the user through the user name, that is, the user's pop user name, password, POP server address, and port number, these four parameters are required to connect to the POP server.
  6. Sqldatareader datareader = This. extgetsetting (username );
  7. If (datareader. Read ())
  8. {
  9. If (datareader ["popserver"]. tostring ()! = "" & Datareader ["popusername"]. tostring ()! = "")
  10. {
  11. // Connect to the POP server
  12. JPOP. connect (datareader ["popusername"]. tostring (), datareader ["poppassword"]. tostring (), datareader ["popserver"]. tostring (), int32.parse (datareader ["popport"]. tostring ()));
  13. // If there is an email on the server
  14. If (JPOP. Count> = 1)
  15. {
  16. For (INT I = 1; I <= JPOP. Count; I ++)
  17. {
  18. MSG = JPOP. Messages [I];
  19. ATTS = msg. attachments;
  20. // Obtain the maximum sending time in the mail information in the database, that is, the time of the last received email.
  21. Database DBASE = new database ();
  22. Datatable DATA = DBASE. getdatatable ("select max (mailsenddate) as mailsenderdate from tabmaillist where mailtypeflag = '1 '");
  23. // Compare the sending time of the email on the server with the time of the last email in the database. If the email is large, it indicates that the email has not been received. It is a new email, this avoids repeated incoming mails.
  24. If (msg. Date> convert. todatetime (data. Rows [0] [0]. tostring ()))
  25. {
  26. // Save the information of this new email to the database
  27. This. saveextmail (MSG, username, datareader ["email"]. tostring (), JPOP. getmessageuid (I ));
  28. // Obtain the attachment and upload it to the server and save the information to the database.
  29. If (ATTS. Count> = 1)
  30. {
  31. For (int K = 0; k <ATTS. Count; k ++)
  32. {
  33. ATT = ATTS [k]; // obtain the attachment
  34. String attname = Att. Name;
  35. Try
  36. {
  37. Random tempnameint = new random ();
  38. String newmaildirname = tempnameint. Next (100000000). tostring ();
  39. Directory. createdirectory (system. Web. httpcontext. Current. server. mappath (".") + "// attachfiles //" + username + "//" + newmaildirname );
  40. String mailpath = "// attachfiles //" + username + "//" + newmaildirname + "//" + attname;
  41. Att. savetofile (system. Web. httpcontext. Current. server. mappath (".") + mailpath );
  42. // Obtain the ID of the email in the database to match the attachment information. obtain the maximum ID in the email table.
  43. Int mailid = This. getmailid ();
  44. // Store the attachment information to the database
  45. This. attextsend (mailid, attname, ATT. Size, mailpath, MSG. From );
  46. }
  47. Catch (exception ex)
  48. {
  49. Throw new exception (ex. Message );
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. // Delete emails from the server
  57. // JPOP. deletemessages ();
  58. // Disconnect
  59. JPOP. Disconnect ();
  60. }
  61. }
  62. }
  63. Email information warehouse receiving:

    1. Public String saveextmail (jmail. Message jmsg, string username, string email)
    2. {
    3. Database Data = new database ();
    4. String mailid = NULL;
    5. Sqlparameter [] prams = {
    6. Data. makeinparam ("@ mailfoldertype", sqldbtype. Int, 20, 1 ),//?
    7. Data. makeinparam ("@ mailreceiverstr", sqldbtype. varchar, 300, username ),//!
    8. Data. makeinparam ("@ mailsenddate", sqldbtype. datetime, 20, datetime. parse (jmsg. Date. tostring (), // sending time
    9. Data. makeinparam ("@ mailsendlevel", sqldbtype. smallint, 20, 1 ),//!
    10. Data. makeinparam ("@ mailsender", sqldbtype. nvarchar, 100, jmsg. From. tostring (), // The sender address
    11. Data. makeinparam ("@ mailreceiver", sqldbtype. nvarchar, 100, username), // recipient
    12. Data. makeinparam ("@ mailsubject", sqldbtype. nvarchar, 150, subject), // mail subject
    13. Data. makeinparam ("@ mailbody", sqldbtype. Text, 300000, (jmsg. bodytext = NULL? "": Body), // mail content
    14. Data. makeinparam ("@ mailcctoaddr", sqldbtype. nvarchar, 300 ,""),//?
    15. Data. makeinparam ("@ mailbcctoaddr", sqldbtype. nvarchar, 300 ,""),//?
    16. Data. makeinparam ("@ mailreadflag", sqldbtype. Bit, 1, 0 ),
    17. Data. makeinparam ("@ mailtypeflag", sqldbtype. Bit, 1, 1 ),//?
    18. Data. makeinparam ("@ mailclassid", sqldbtype. Int, 1, 0 ),
    19. Data. makeinparam ("@ mailimportance", sqldbtype. Int, 1, convert. toint32 (jmsg. Priority), // priority
    20. Data. makeoutparam ("@ mailid", sqldbtype. Int, 20 )//?
    21. };
    22. Try
    23. {
    24. Data. runproc ("sp_mailsend", prams );
    25. Mailid = prams [14]. value. tostring ();
    26. If (mailid = string. Empty)
    27. Return NULL;
    28. Else
    29. Return mailid;
    30. }
    31. Catch (exception ex)
    32. {
    33. Error. Log (ex. tostring ());
    34. }
    35. }

     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.