Amazon web services 如何在golang中集成aws sdk ses?
我正在使用AWS以Go语言托管我的服务器。我被卡住了,因为我不知道如何使用他们的电子邮件。有什么想法吗 如您问题的链接所示,这非常简单 你有什么问题 最简单的例子: 导入:Amazon web services 如何在golang中集成aws sdk ses?,amazon-web-services,go,amazon-ses,Amazon Web Services,Go,Amazon Ses,我正在使用AWS以Go语言托管我的服务器。我被卡住了,因为我不知道如何使用他们的电子邮件。有什么想法吗 如您问题的链接所示,这非常简单 你有什么问题 最简单的例子: 导入:github.com/aws/aws sdk go/aws,github.com/aws/aws sdk go/service/ses和github.com/aws/aws sdk go/aws/credentials,github.com/aws/aws sdk go/aws/session awsSession := se
github.com/aws/aws sdk go/aws
,github.com/aws/aws sdk go/service/ses
和github.com/aws/aws sdk go/aws/credentials
,github.com/aws/aws sdk go/aws/session
awsSession := session.New(&aws.Config{
Region: aws.String("aws.region"),
Credentials: credentials.NewStaticCredentials("aws.accessKeyID", "aws.secretAccessKey" , ""),
})
sesSession := ses.New(awsSession)
sesEmailInput := &ses.SendEmailInput{
Destination: &ses.Destination{
ToAddresses: []*string{aws.String("receiver@xyz.com")},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Data: aws.String("Body HTML")},
},
Subject: &ses.Content{
Data: aws.String("Subject"),
},
},
Source: aws.String("sender@xyz.com"),
ReplyToAddresses: []*string{
aws.String("sender@xyz.com"),
},
}
_, err := sesSession.SendEmail(sesEmailInput)
有关详细记录的示例,请参阅此处:
主程序包
进口(
“fmt”
//go get-u github.com/aws/aws-sdk-go
“github.com/aws/aws sdk go/aws”
“github.com/aws/aws sdk go/aws/session”
“github.com/aws/aws sdk go/service/ses”
“github.com/aws/aws sdk go/aws/awserr”
)
常数(
//替换sender@example.com用你的“发件人”地址。
//必须通过Amazon SES验证此地址。
发送方=”sender@example.com"
//替换recipient@example.com带有“收件人”地址。如果您的帐户
//仍在沙箱中,必须验证此地址。
收件人=”recipient@example.com"
//指定配置集。如果不想使用配置
//设置并注释以下常量和
//ConfigurationSetName:aws.String(ConfigurationSet)参数如下
ConfigurationSet=“ConfigSet”
//将us-west-2替换为您用于Amazon SES的AWS区域。
AwsRegion=“us-west-2”
//电子邮件的主题行。
主题=“亚马逊SES测试(AWS SDK for Go)”
//电子邮件的HTML正文。
HtmlBody=“Amazon SES测试电子邮件(AWS SDK for Go)此电子邮件与一起发送”+
“使用”+
“”
//具有非HTML电子邮件客户端的收件人的电子邮件正文。
TextBody=“此电子邮件是使用AWS SDK for Go与Amazon SES一起发送的。”
//电子邮件的字符编码。
CharSet=“UTF-8”
)
func main(){
//创建新会话并指定AWS区域。
sess,err:=session.NewSession(&aws.Config){
区域:aws.String(AwsRegion)},
)
//在会话中创建SES客户端。
svc:=ses.New(sess)
//收集电子邮件。
输入:=&ses.SendEmailInput{
目的地:&ses.Destination{
CCAddress:[]*字符串{
},
ToAddress:[]*字符串{
aws.String(收件人),
},
},
消息:&ses.Message{
Body:&ses.Body{
Html:&ses.Content{
字符集:aws.String(字符集),
数据:aws.String(HtmlBody),
},
文本:&ses.Content{
字符集:aws.String(字符集),
数据:aws.String(TextBody),
},
},
主题:&ses.Content{
字符集:aws.String(字符集),
数据:aws.String(主题),
},
},
来源:aws.String(发送方),
//如果未使用配置集,请注释或删除以下行
ConfigurationSetName:aws.String(ConfigurationSet),
}
//尝试发送电子邮件。
结果,错误:=svc.sendmail(输入)
//如果出现错误,则显示错误消息。
如果错误!=零{
如果aerr,ok:=错误(awserr.Error);ok{
开关电源代码(){
案例ses.errCodeMessage被拒绝:
fmt.Println(ses.ErrCodeMessageRejected,aer.Error())
案例ses.ErrCodeMailFromDomainNotVerifiedException:
fmt.Println(ses.ErrCodeMailFromDomainNotVerifiedException,aer.Error())
案例ses.ErrCodeConfigurationSetDoesNotExistException:
fmt.Println(ses.ErrCodeConfigurationSetDoesNotExistException,aer.Error())
违约:
fmt.Println(aerr.Error())
}
}否则{
//打印错误,将err强制转换为awserr.error以获取代码和
//来自错误的消息。
fmt.Println(err.Error())
}
返回
}
fmt.Println(“发送电子邮件!”)
fmt.Println(结果)
}
请参阅:API是否已更改?从本地主机尝试此代码时,我收到错误MissingEndpoint:“端点”配置是此服务所必需的。
package main
import (
"fmt"
//go get -u github.com/aws/aws-sdk-go
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/aws/aws-sdk-go/aws/awserr"
)
const (
// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
Sender = "sender@example.com"
// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
Recipient = "recipient@example.com"
// Specify a configuration set. If you do not want to use a configuration
// set, comment out the following constant and the
// ConfigurationSetName: aws.String(ConfigurationSet) argument below
ConfigurationSet = "ConfigSet"
// Replace us-west-2 with the AWS Region you're using for Amazon SES.
AwsRegion = "us-west-2"
// The subject line for the email.
Subject = "Amazon SES Test (AWS SDK for Go)"
// The HTML body for the email.
HtmlBody = "<h1>Amazon SES Test Email (AWS SDK for Go)</h1><p>This email was sent with " +
"<a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the " +
"<a href='https://aws.amazon.com/sdk-for-go/'>AWS SDK for Go</a>.</p>"
//The email body for recipients with non-HTML email clients.
TextBody = "This email was sent with Amazon SES using the AWS SDK for Go."
// The character encoding for the email.
CharSet = "UTF-8"
)
func main() {
// Create a new session and specify an AWS Region.
sess, err := session.NewSession(&aws.Config{
Region:aws.String(AwsRegion)},
)
// Create an SES client in the session.
svc := ses.New(sess)
// Assemble the email.
input := &ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: []*string{
},
ToAddresses: []*string{
aws.String(Recipient),
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(HtmlBody),
},
Text: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(TextBody),
},
},
Subject: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(Subject),
},
},
Source: aws.String(Sender),
// Comment or remove the following line if you are not using a configuration set
ConfigurationSetName: aws.String(ConfigurationSet),
}
// Attempt to send the email.
result, err := svc.SendEmail(input)
// Display error messages if they occur.
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
fmt.Println(ses.ErrCodeMessageRejected, aerr.Error())
case ses.ErrCodeMailFromDomainNotVerifiedException:
fmt.Println(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
case ses.ErrCodeConfigurationSetDoesNotExistException:
fmt.Println(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println("Email Sent!")
fmt.Println(result)
}