【问题标题】:How to send email with attachment xamarin forms如何发送带有附件 xamarin 表单的电子邮件
【发布时间】:2018-04-26 07:35:15
【问题描述】:

C#:

private  void SendEmail(object sender, EventArgs e)
        {
            string subject = "subject here ";
            string body = "body here ";
                var mail = new MailMessage();
                var smtpServer = new SmtpClient("smtp.gmail.com", 587);
                mail.From = new MailAddress("veezo2007pk@gmail.com");
                mail.To.Add("veezo2009pk@gmail.com");
                mail.Subject = subject;
                mail.Body = body;
                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment();
                mail.Attachments.Add(attachment);
                smtpServer.Credentials = new NetworkCredential("veezo2007pk", "password");

                smtpServer.UseDefaultCredentials = false;
                smtpServer.EnableSsl = true;
                smtpServer.Send(mail);

        }

private async void File(object sender, EventArgs e)
        {


          var file = await CrossFilePicker.Current.PickFile();                      

            if (file != null)
            {
                fileLabel.Text = filepath;
            }
        }

XAML:

 <Entry Placeholder="Phone No" x:Name="Phone" />  
                <Button Text="Pick a file" x:Name="fileButton" Clicked="File"/>
                    <Label Text="This is FileName" x:Name="fileLabel"/>    
                <Button Text="Submit" Clicked="SendEmail" BackgroundColor="Crimson" TextColor="White" WidthRequest="100" />

我想发送一封带有附件的电子邮件。如果我删除附件的代码,电子邮件就会发送给收件人。当我使用附件代码时,不会发送电子邮件。不知道为什么……

【问题讨论】:

标签: email xamarin xamarin.forms smtp system.net.mail


【解决方案1】:

它失败了,因为您从来没有向附件变量添加文件:

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment();
mail.Attachments.Add(attachment);

【讨论】:

    【解决方案2】:

    您需要在新的System.Net.Mail.Attachment构造函数的参数中添加文件路径

    attachment = new System.Net.Mail.Attachment("enter file path here");
    

    但是,还有其他选择。见this Microsoft documentation。 您可以输入 Stream 而不是字符串(文件路径)。

    我认为以下内容可能对您有用:

    attachment = new System.Net.Mail.Attachment(fileLabel.Text);
    

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 2013-02-23
      • 2015-09-09
      • 2022-01-17
      • 1970-01-01
      • 2012-06-07
      相关资源
      最近更新 更多