Golang using Lxn/walk to do GUI interface development: e-Mail Sender

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

I am also just using walk this component, a lot of control interface is not familiar with, my last sent the bulk of the message to the graphical interface, thread to send mail, GOB library to save data. Only available for learning communication, for reference only (the interface sucks).

Next is the code for MAIN.GO:

 Multi-SendEmail project main.gopackage mainimport  (     "Bufio"      "Encoding/gob"      "errors"      "FMT"       "io"      "net/smtp"      "OS"      " StrConv "    " strings "    " Time "import  (    " Github.com/lxn/walk "    . " github.com/lxn/walk/declarative ") type shuju  struct {    name    string    pwd      string    Host    string     Subject string    body    string    send     string}func sendmail (user, password, host, to, subject,  Body, mailtype striNG)  error {    fmt. Println ("send to "  + to)     //fmt. Println (User, password, host, to, subject, body, mailtype)      hp := strings. Split (host,  ":")     auth := smtp. Plainauth ("",  user, password, hp[0])     var content_type string     if mailtype ==  "HTML"  {         content_type =  "Content-type: text/html;charset=utf-8"     } else  {        content_type =  "Content-type: text/plain ; Charset=utf-8 "    }    body = strings. Trimspace (body)     msg := []byte ("to: "  + to +  "\r\ nfrom:  " + user +&nbsP; " < " + user + " >\r\nSubject:  " + subject + " \ r \ n " +  content_type +  "\r\n\r\n"  + body)     send_to :=  Strings. Split (to,  ";")     err := smtp. SendMail (host, auth, user, send_to, msg)     if err !=  Nil {        fmt. Println (Err. Error ())     }    return err}func readline2array (filename  string)   ([]string, error)  {    result := make ([]string,  0)     file, err := os. Open (filename)     if err != nil {         return result, errors. New ("open file failed.")     }    defer file. Close()     bf := bufio. Newreader (file)     for {        line,  ISPREFIX,&NBSP;ERR1&NBSP;:=&NBSP;BF. ReadLine ()         if err1 != nil {             if err1 != io. eof {                 Return result, errors. New ("Readline no finish")                          break         }        if isPrefix {             return result, errors. New ("Line is too long")     &nbsP;   }        str := string (line)          result = append (RESULT,&NBSP;STR)     }     return result, nil}func delarrayvar (arr []string, str string)  []string {    str = strings. Trimspace (str)     for i, v := range arr {         v = strings. Trimspace (v)         if v == str {             if i == len (arr)  {                 return arr[0 :  i-1]            }             if i == 0 {                 return arr[1:len (arr)]             }             a1 := arr[0:i]            a2  := arr[i+1 : len (arr)]             return append (A1, a2 ...)         }    }    return  Arr}func loaddata ()  {    fmt. Println ("LoadData")     file, err := os. Open ("Data.dat")     defer file. Close ()     if err != nil {         fmt.Println (Err. Error ())         sj. name =  "User name"         SJ.Pwd =  "User password"    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. host =  "SMTP server: Port" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. subject =  "Mail Subject" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. body =  "Mail Content" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. send =  "The mailbox to be sent, one per line" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;&NBSP;&NBSP;&NBSP;}     dec := gob. Newdecoder (file)     err2 := dec. Decode (&AMP;SJ)     if err2 != nil {         fmt. Println (ERR2. Error ())         sj. name =  "User name"         SJ.Pwd =  "User password"         &NBSP;SJ. host =  "SMTP server: Port" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. subject =  "Mail Subject" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. body =  "Mail Content" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. send =  "The mailbox to be sent, one per line"     }}func savedata ()  {    fmt . Println ("SaveData")     file, err := os. Create ("Data.dat")     defer file. Close ()     if err != nil {         fmt. PRINTLN (Err)     }    enc := gob. Newencoder (file)     err2 := enc. Encode (SJ)     if err2 != nil {         fmt. Println (ERR2)     }}var sj shujuvar runing boolvar chend chan  boolfunc maiN ()  {    loaddata ()     chend = make (Chan bool)     var emails, body, msgbox *walk. Textedit    var user, password, host, subject *walk. Lineedit    var startbtn *walk. pushbutton    mainwindow{        title:     "e-mail generator  By  a sad",         minsize: size{800,  600},        Layout:  HBox{},         Children: []Widget{           &NBSP;&NBSP;TEXTEDIT{ASSIGNTO:&NBSP;&AMP;EMAILS,&NBSP;TEXT:&NBSP;SJ. send, tooltiptext:  "outgoing mailing list, one per line"},             vsplitter{                children: []widget{                     lineedit{ ASSIGNTO:&NBSP;&AMP;USER,&NBSP;TEXT:&NBSP;SJ. name, cuebanner:  "Please enter the mailbox user name"},                     LineEdit{AssignTo: &password, Text:  Sj. pwd, passwordmode: true, cuebanner:  "Please enter your email login password"},                     LineEdit{AssignTo:  &AMP;HOST,&NBSP;TEXT:&NBSP;SJ. host, cuebanner:  "SMTP server: Port"},                     lineedit{assignto: &subject, text: sj . subject, cuebanner:  "Please enter message subject ..."},&NBSP;&NBSP;&NBSP;&Nbsp;                textedit{ ASSIGNTO:&NBSP;&AMP;BODY,&NBSP;TEXT:&NBSP;SJ. body, tooltiptext:  "Please enter message content", columnspan: 2},                     TextEdit{AssignTo: & msgbox, readonly: true},                     PushButton{                         assignto:  &startBtn,                         Text:      "Start mass",                         onclicked: func ()  {                           &NBSP;&NBSP;&NBSP;SJ. Name = user. Text ()                   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. Pwd = password. Text ()                   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. Host = host. Text ()                   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. Subject = subject. Text ()                       &nbsP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. Body = body. Text ()                   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. Send = emails. Text ()                              savedata ()                               if runing == false {                                  runing = true                      &nbSp;          startbtn.settext ("Stop sending")                                   go sendthread (Msgbox, emails)                              } else {                                  runing = false                                  startbtn.settext ("Start mass")                              }                          },                     },                 },             },        },    }. Run ()}func sendthread (Msgbox, es *walk. TextEdit)  {    sendto := strings. Split (SJ. send,  "\ r \ n")     susscess := 0    count :=  Len (sendTo)     for index, to := range sendTo {         if runing == false {             break        }         MsgBox. SetText ("Send To"  + to +  "..."  + strconv. Itoa ((index/count) *100)  +  "%")         err :=  SendMail (SJ). NAME,&NBSP;SJ. PWD,&NBSP;SJ. HOST,&NBSP;TO,&NBSP;SJ. SUBJECT,&NBSP;SJ. body,  "html")         if err != nil {             msgbox. AppendText ("\ r \ n failed:"  + err. Error ()  +  "\ r \ n")             if err . Error ()  ==  "550 mailbox not found or access denied"  {                 sj. Send = strings. Join (Delarrayvar (strings). Split (SJ. send,  "\ r \ n"),  to),  "\ r \ n")                  es. SetText (SJ). Send)             }             time. Sleep (1 * time. Second)             continue         } else {             susscess++            msgbox. AppendText ("\ r \ n sent successfully!") &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SJ. Send = strings. Join (Delarrayvar (strings). Split (SJ. send,  "\ r \ n"),  to),  "\ r \ n")              es. SetText (SJ). Send) &nbsP;       }        time. Sleep (1 * time. Second)     }    savedata ()     msgbox. AppendText ("Stop sending! Success  "  + strconv. Itoa (susscess)  +  "  \ r \ n")}

Then is the walk program used by the Main.manifest, completely copied the official, has not studied the meaning:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"? ><assembly xmlns= "Urn:schemas-microsoft-com:asm.v1" Manifestversion= "1.0" > <assemblyidentity version= "1.0.0.0" processorarchitecture= "*" Name= "Somefunkynamehere "Type=" Win32 "/> <dependency> <dependentAssembly> <assemblyidentity type=" Win32 "NA Me= "Microsoft.windows.common-controls" version= "6.0.0.0" processorarchitecture= "*" publickeytoken= " 6595B64144CCF1DF "language=" * "/> </dependentAssembly> </dependency></assembly>

Execute with this main.manifest (if walk is installed, this command should be available):

Rsrc-manifest Main.manifest-o Rsrc.syso

is a list of parameters for the RSRC command, such as I add an ico icon to my own program:

Rsrc-manifest Main.manifest–ico Icon.ico-o Rsrc.syso

Summary: As long as you are familiar with these very powerful third-party libraries, go should also be able to make a very good graphics application, for contact C, C + + late friends are very suitable.


===================== International Practice =======================

Just started to learn to write some blog, write a bad place please forgive me, feel that learned to pay attention to the next bar ~

My QQ group (PHP) 336533596,qq 451309839, Welcome to join. Communicate with each other, chat and brag ~

Related Article

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.