Advertisement
alpa_s

Untitled

Oct 4th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.  
  5. "log"
  6. "net/mail"
  7. "encoding/base64"
  8. "net/smtp"
  9. "fmt"
  10. "strings"
  11. )
  12.  
  13. func encodeRFC2047(String string) string{
  14. // use mail's rfc2047 to encode any string
  15. addr := mail.Address{String, ""}
  16. return strings.Trim(addr.String(), " <>")
  17. }
  18.  
  19.  
  20. func main() {
  21. // Set up authentication information.
  22.  
  23. smtpServer := "smtp.yandex.ru"
  24. auth := smtp.PlainAuth(
  25. "",
  26. "alpa.92@yandex.ru",
  27. "*****",
  28. smtpServer,
  29. )
  30.  
  31. from := mail.Address{"Alex", "alpa.92@yandex.ru"}
  32. to := mail.Address{"Alex", "pastuhov.alexandr@gmail.com"}
  33. title := "Hello Alex"
  34.  
  35. body := "Hello Alex, this is test mail"
  36.  
  37. header := make(map[string]string)
  38. header["From"] = from.String()
  39. header["To"] = to.String()
  40. header["Subject"] = encodeRFC2047(title)
  41. header["MIME-Version"] = "1.0"
  42. header["Content-Type"] = "text/plain; charset=\"utf-8\""
  43. header["Content-Transfer-Encoding"] = "base64"
  44.  
  45. message := ""
  46. for k, v := range header {
  47. message += fmt.Sprintf("%s: %s\r\n", k, v)
  48. }
  49. message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
  50.  
  51. // Connect to the server, authenticate, set the sender and recipient,
  52. // and send the email all in one step.
  53. err := smtp.SendMail(
  54. smtpServer + ":25",
  55. auth,
  56. from.Address,
  57. []string{to.Address},
  58. []byte(message),
  59. //[]byte("This is the email body."),
  60. )
  61. if err != nil {
  62. log.Fatal(err)
  63. } else {
  64. fmt.Println("all is ok")
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement