Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import("fmt"
- "strings"
- "net"
- "net/smtp"
- "log"
- )
- var(
- hostlist = make([]string,0)
- result = ""
- err error
- )
- func main(){
- var toemail,fromemail string = "bharatsewani1993@gmail.com","bharatsewani@gmail.com"
- //var toemail string = "bharatsewani1993@gmail.com"
- // var getdetails bool
- //get the domain name from email recipient
- email_arr := strings.Split(toemail,"@")
- domain := email_arr[1]
- //get mx record of domain
- mxhosts,err := net.LookupMX(domain)
- if err != nil{
- panic(err)
- }
- //if mx record not found get dns record of domain
- if len(mxhosts) < 1 {
- dnshosts,err := net.LookupNS(domain)
- if err != nil{
- panic(err)
- }
- if len(dnshosts) > 0{
- for i := 0; i < len(dnshosts); i++ {
- hostlist = append(hostlist,dnshosts[i].Host)
- }
- }
- } else {
- //found mxhosts
- for i := 0; i < len(mxhosts); i++ {
- hostlist = append(hostlist,mxhosts[i].Host)
- }
- }
- //check if host list is not empty
- if len(hostlist) > 0{
- ip := hostlist[0]
- //connect to smtp server
- c, err := smtp.Dial(ip+":25")
- if err != nil {
- log.Fatal(err)
- }
- hello := c.Hello(ip)
- from := c.Mail(fromemail)
- to := c.Rcpt(toemail)
- quit := c.Quit()
- fmt.Println("Hello ==> ",hello)
- fmt.Println("From ==> ",from)
- fmt.Println("to ==> ",to)
- fmt.Println("Quit ==>", quit)
- // Send the email body.
- } else {
- fmt.Println("invalid")
- fmt.Println("No suitable mx record found!")
- }
- }
Add Comment
Please, Sign In to add comment