Here we are going to compose an email from a Universal App with email compose class.
Step 1: Open a blank window and add the following fields either by using toolbox or by copying the following XAML code to your grid.
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="Auto"></ColumnDefinition>
- <ColumnDefinition Width="*"></ColumnDefinition>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- <RowDefinition Height="Auto"></RowDefinition>
- </Grid.RowDefinitions>
- <TextBlock Text="Compose Mail" FontSize="25" Grid.Row="0" FontWeight="Bold"></TextBlock>
- <TextBlock Margin="5,10,0,0" Text="To" Grid.Row="1" FontSize="20"></TextBlock>
- <TextBox Margin="0,10,10,0" Name="to" Grid.Column="1" Grid.Row="1"></TextBox>
- <TextBlock Margin="5,10,0,0" Text="CC" Grid.Row="2" FontSize="20"></TextBlock>
- <TextBox Margin="0,10,10,0" Name="cc" Grid.Column="1" Grid.Row="2"></TextBox>
- <TextBlock Margin="5,10,0,0" Text="Subject" Grid.Row="3" FontSize="20"></TextBlock>
- <TextBox Margin="0,10,10,0" Name="subject" Grid.Column="1" Grid.Row="3"></TextBox>
- <TextBlock Margin="5,10,0,0" Text="Body" Grid.Row="4" FontSize="20"></TextBlock>
- <TextBox Margin="0,10,10,0" Name="body" Grid.Column="1" Grid.Row="4" Height="100" TextWrapping="Wrap"></TextBox>
- <Button Margin="0,10,10,0" HorizontalAlignment="Right" Content="Send" Name="sendEmail" Grid.Column="1" Grid.Row="5" Click="sendEmail_Click"></Button>
Step 2: Copy and paste the following code to the cs page which will handle each event assigned to the Send Button.
- private async void sendEmail_Click(object sender, RoutedEventArgs e)
- {
- EmailMessage email = new EmailMessage();
- email.To.Add(new EmailRecipient(to.Text));
- email.CC.Add(new EmailRecipient(cc.Text));
- email.Subject = subject.Text;
- email.Body = body.Text;
- await EmailManager.ShowComposeNewEmailAsync(email);
- }
Step 3: Run and test your application.