zoukankan      html  css  js  c++  java
  • Golang Email

    Backup Code

    I dont have chinese inputmethod now ( what the fuck about Linux KDE envirnment !) , so just leave some message for this backup code about golang email . I remember that I had written this function a long time ago , but I can not find it out just now . Some code was searched on Internet .

    This is a example for QQ email . But you can use other email ,eg 163,yahoo... But you must config the server smtp address .

    Here it is :

    // EmailMain
    package main
    
    import (
    	"fmt"
    	"net/smtp"
    	"strings"
    )
    
    const(
    	HOST = "smtp.qq.com"
    	SERVER_ADDR = "smtp.qq.com:25"
    	USER = "624432528@qq.com"
    	PASSWORD = "xxxx"
    )
    
    type Email struct {
    	to string "to"
    	subject string "subject"
    	msg string "msg"
    }
    
    func NewEmail(to,subject,msg string) *Email{
    	return &Email{to:to,subject:subject,msg:msg}
    }
    
    func SendEmail(email *Email) error{
    	auth := smtp.PlainAuth("",USER,PASSWORD,HOST)
    	sendTo := strings.Split(email.to,";")
    	done := make(chan error,1024)
    	
    	go func(){
    		defer close(done)
    		for _,v := range sendTo {
    			//warning ; the last 
     need twice , not only one .
    			str := strings.Replace("From:"+USER+"~To :"+v+"~Subject:"+email.subject+"~Content-Type: text/plain;charset=UTF-8~","~","
    ",-1)+"
    "+email.msg
    			fmt.Println("Content:",str)
    			err := smtp.SendMail(SERVER_ADDR,auth,USER,[]string{v},[]byte(str))
    			if err != nil{
    				fmt.Println("Send Error:",err)
    			}
    			done <- err
    		}
    	}()
    		
    	for i:=0;i<len(sendTo);i++{
    		<- done
    	}
    
    	return nil
    }
    
    func main() {
    	email := NewEmail("798207330@qq.com","How Are you","Hello World , I am wang")
    	err := SendEmail(email)
    	fmt.Println("result:",err)
    }
    
    
  • 相关阅读:
    linux 学习随笔-shell基础知识
    linux 学习随笔-压缩和解压缩
    解析xml的4种方法详解
    集合工具类
    Map概述
    List集合概述
    Java集合框架
    Spring JdbcTemplate详解
    关于c3p0数据库连接池的简单使用
    Java通过JDBC封装通用DAO层
  • 原文地址:https://www.cnblogs.com/juepei/p/4612067.html
Copyright © 2011-2022 走看看